Example call data from API.
Guide API.
- 1. Initialize a new cURL session using the curl_init() function.
- 2. Set the request URL using the curl_setopt() function with the CURLOPT_URL option.
- 3. Set the request method to POST using the curl_setopt() function with the CURLOPT_POST option.
- 4. Set the request body as a URL-encoded string of parameters using the curl_setopt() function with the CURLOPT_POSTFIELDS option. This can be a string of data or an array of key-value pairs.
- 5. Set any required headers using the curl_setopt() function with the CURLOPT_HTTPHEADER option. This can be an array of strings, each representing a single header.
- 6. Set the CURLOPT_RETURNTRANSFER option to true to ensure that the response is returned as a string.
Here's an example code snippet that shows how to use PHP cURL to make a POST request with URL parameters to an API:
// API endpoint URL, Token, Secret key, hscode
$url_api = https://tip.mci.gov.tl/index.php/en-gb/api/commodity;
$api_token = "xxxxxxxx";
$secret_key = "xxxxxx";
$hscode = "xxxxxx";
// Request body parameters
$data = ['api_token' => $api_token, 'secret_key' => $secret_key, 'hscode' => $hscode];
// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_api);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute request and get response
$result = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Do something with the response data
echo $result;
This code is an example of how to send a POST request to an API endpoint using the HTTPS module in Node.js.
- 1. The code starts by requiring the https module, which provides an interface for making HTTPS requests.
- 2. It then sets the URL of the API endpoint, the data to be sent in the request, and the options for the request. The data is provided as an object with three properties: api_token, secret_key, and hscode.
- 3. The options object specifies that the method of the request should be POST, and sets the Content-Type header to application/json.
- 4. The code then creates a request object using the https.request() method, passing in the URL, options, and a callback function to handle the response. The callback function listens for the data and end events on the response object, concatenating the chunks of data that are received into a string, and logging the final response to the console.
- 5. If an error occurs, the code logs the error to the console.
- 6. Finally, the data is sent in JSON format using the req.write() method and the request is ended with req.end().
const https = require('https');
// the URL of your API endpoint
const url_api = https://tip.mci.gov.tl/index.php/en-gb/api/commodity;
// the data to be sent in the POST request
const data = {
api_token : 'xxxx',
secret_key : 'xxxx',
hscode :'xxxxxx'
};
const options = { method: 'POST',
headers: {
'Content-Type': 'application/json'
} };
const req = https.request(url, options, (res) => {
let response = '';
res.on('data', (chunk) => {
response += chunk;
}); res.on('end', () => {
console.log(response);
});
});
req.on('error', (error) => {
console.error(error);
});
// send the data in JSON format
req.write(JSON.stringify(data));
req.end();
// the URL of your API endpoint
const url_api = https://tip.mci.gov.tl/index.php/en-gb/api/commodity;
// the data to be sent in the POST request
const data = {
api_token : 'xxxx',
secret_key : 'xxxx',
hscode :'xxxxxx'
};
const options = { method: 'POST',
headers: {
'Content-Type': 'application/json'
} };
const req = https.request(url, options, (res) => {
let response = '';
res.on('data', (chunk) => {
response += chunk;
}); res.on('end', () => {
console.log(response);
});
});
req.on('error', (error) => {
console.error(error);
});
// send the data in JSON format
req.write(JSON.stringify(data));
req.end();
- 1. In this example, url is the API endpoint you want to send a POST request to. payload is the data you want to send in the request body. The json.dumps() function is used to convert the payload to a JSON string. headers specifies that the request body is in JSON format. The requests.post() method is used to make the POST request with the specified headers and JSON payload.
- 2. Finally, response.json() is used to get the JSON response from the API endpoint, and the response is printed to the console using print(json_response).
import requests
import json
url_api = https://tip.mci.gov.tl/index.php/en-gb/api/commodity
# Define the payload for the POST request
payload = {
api_token : 'xxxx',
secret_key : 'xxxx',
hscode :'xxxxxx'
}
# Convert the payload to JSON
json_payload = json.dumps(payload)
# Set the headers
headers = {
'Content-Type': 'application/json'
}
# Make the POST request
response = requests.post(url_api, headers=headers, data=json_payload)
# Get the JSON response
json_response = response.json()
# Print the JSON response
print(json_response)
import json
url_api = https://tip.mci.gov.tl/index.php/en-gb/api/commodity
# Define the payload for the POST request
payload = {
api_token : 'xxxx',
secret_key : 'xxxx',
hscode :'xxxxxx'
}
# Convert the payload to JSON
json_payload = json.dumps(payload)
# Set the headers
headers = {
'Content-Type': 'application/json'
}
# Make the POST request
response = requests.post(url_api, headers=headers, data=json_payload)
# Get the JSON response
json_response = response.json()
# Print the JSON response
print(json_response)
step-by-step explanation of how to use cURL command to make a POST request to API endpoint and
receive a JSON response in different programming languages:
-
Step 1: Define the API Endpoint
The first step is to define the API endpoint you want to send a POST request to. This could be any URL that accepts POST requests. - Step 2: Define the Request Payload
The next step is to define the data you want to send in the request body. This could be any valid JSON payload that the API endpoint expects. - Step 3: Define the Request Headers:
The request headers tell the API endpoint what kind of data is being sent in the request body. In this case, you'll want to set the Content-Type header to application/json to indicate that the request body is in JSON format. - Step 4: Generate the cURL Command
Once you have defined the API endpoint, request payload, and headers, you can generate the cURL command. The exact command will depend on the programming language you're using.
Note: for 'url_api', 'api_token' and 'secret_key' contact us with administrator generate for you.