POST ticket using cURL in DRE
This example script utilizes cURL (client URL), a versatile command-line tool for transferring data to and from a server, which is supported on the majority of devices. cURL can be employed in various ways, including but not limited to batch or shell scripts. For this example we will use the ticket API endpoint to create a deferred support request. This call requires a public API key and returns the ticket number when successful.
Avoid hardcoding API keys directly in your scripts for production applications. Instead, consider using environment variables or securely retrieving the keys from a server-side application.
We’ll begin by presenting a basic cURL script example. Following that, we’ll demonstrate how to implement the script as a Windows Batch and macOS/Linux Shell script.
Basic cURL script
curl --location '<Your_domain_for_APIs>/ticket' \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header 'INTEGRATION-KEY: <my_public_api_key>' \ --data-raw '{ "ids": <enter the unique reference number of Take Control Agent>, "customer_name": "<enter customer's name>", "customer_email": "<enter customer's email address>", "problem_description": "<enter a description of the issue>" }'
Breakdown
curl --location
|
curl initiates an HTTP request and --location tells cURL to follow any redirects, which is useful when the server might redirect the request to a different URL. |
--header 'Content-Type: application/json'
|
This header indicates that the data being sent in the request is in JSON format. |
--header 'Accept: application/json'
|
This header tells the server that the client expects the response to be in JSON format. |
--header 'INTEGRATION-KEY: <my_public_api_key>'
|
Authenticates the API call by adding the INTEGRATION-KEY header. |
--data-raw
|
Allows you to send raw data in the body of the request. In this case, it’s a JSON object. |
Using the cURL request in a Batch or Shell script
Wrapping a cURL request in a batch (for Windows) or shell script (for Linux or macOS) allows you to automate the process of sending requests and handling responses.
These take the core script cURL described above, tweaked to match the script format, and includes error message handling.
Batch script for Windows (.bat)
@echo off setlocal enabledelayedexpansion :: Set variables set "url=<Your_domain_for_APIs>/ticket" set "content_type=application/json" set "accept=application/json" set "integration_key=<my_public_api_key>" set "ids=<enter the unique reference number of Take Control Agent>" set "customer_name=<enter customer's name>" set "customer_email=<enter customer's email address>" set "problem_description=<enter a description of the issue>" :: Create JSON data set "json_data={\"ids\": !ids!,\"customer_name\":\"!customer_name!\",\"customer_email\":\"!customer_email!\",\"problem_description\":\"!problem_description!\"}" :: Execute cURL command curl --location "!url!" ^ --header "Content-Type: !content_type!" ^ --header "Accept: !accept!" ^ --header "INTEGRATION-KEY: !integration_key!" ^ --data-raw "!json_data!" endlocal
Breakdown of the batch script for Windows
@echo off
|
Prevents the commands from displaying as they run. |
setlocal enabledelayedexpansion
|
Allows the use of ! for variable expansion within the script. |
Set variables
|
Defines variables for the URL, headers, and JSON data elements. Replace these placeholders with your actual values. |
Create JSON data
|
Builds the JSON data as a single line, ensuring double quotes are escaped. |
Execute cURL command
|
Uses the cURL command with the specified headers and JSON data. |
endlocal
|
Ends the localization of environment changes. |
Shell script for macOS and Linux (.sh)
#!/bin/bash # Set variables url="<Your_domain_for_APIs>/ticket" content_type="application/json" accept="application/json" integration_key="<my_public_api_key>" ids="<enter the unique reference number of DRE Agent>" customer_name="<enter customer's name>" customer_email="<enter customer's email address>" problem_description="<enter a description of the issue>" # Create JSON data json_data=$(cat <<EOF { "ids": "$ids", "customer_name": "$customer_name", "customer_email": "$customer_email", "problem_description": "$problem_description" } EOF ) # Execute cURL command curl --location "$url" \ --header "Content-Type: $content_type" \ --header "Accept: $accept" \ --header "INTEGRATION-KEY: $integration_key" \ --data-raw "$json_data"
If you encounter issues running the shell script, you may need to make it executable. To do this, open your terminal and run the following command, replacing <script_name>
with the name of your .sh script:
chmod +x <script_name>
Breakdown of the Shell script for macOS and Linux
@echo off
|
Batch | Prevents the commands from displaying as they run. |
#!/bin/bash
|
Shell | Specifies that the script should be run using the Bash shell. |
setlocal enabledelayedexpansion
|
Batch | Allows the use of ! for variable expansion within the script. |
Set variables
|
Both | Defines variables for the URL, headers, and JSON data elements. Replace these placeholders with your actual values. |
Create JSON data
|
Both | Builds the JSON data as a single line, ensuring double quotes are escaped. |
Execute cURL command
|
Both | Uses the cURL command with the specified headers and JSON data. |
endlocal
|
Batch | Ends the localization of environment changes. |