HttpClient_SendRequest
This function initiates an HTTP request to a specified URL, sending it to the targeted web service. Once the response is received, the function triggers the provided callback, allowing you to handle the result asynchronously. You can customize the request by specifying various parameters, such as HTTP method, headers, and request body, ensuring flexibility for different use cases.
Function
//ARGS:
//0: Url
//1: Method
//2: ResultCallback
//3: Body [Optional]
//4: Headers [Optional]
//5: Thumbprint [Optional]
//6: CaCertPath [Optional]
void HttpClient_SendRequest(url : string, method : string, responseCallback : string);
void HttpClient_SendRequest(url : string, method : string, responseCallback : function);
void HttpClient_SendRequest(url : string, method : string, responseCallback : string, body : string, headers : array);
void HttpClient_SendRequest(url : string, method : string, responseCallback : function, body : string, headers : string array);
void HttpClient_SendRequest(url : string, method : string, responseCallback : string, body : string, headers : array, thumbprint : string);
void HttpClient_SendRequest(url : string, method : string, responseCallback : function, body : string, headers : string array, thumbprint : string);
void HttpClient_SendRequest(url : string, method : string, responseCallback : string, body : string, headers : array, thumbprint : string, caPath : string);
void HttpClient_SendRequest(url : string, method : string, responseCallback : function, body : string, headers : string array, thumbprint : string, caPath : string);
void HttpRequest_Callback(code, headers, body);
Parameters
Name | Type | Description |
---|---|---|
url | String | The URL endpoint to which the HTTP request will be sent. |
method | String | The HTTP method to use for the request. Supported methods: ‘GET’, ‘POST’, ‘PUT’, ‘DELETE’, ‘PATCH’. |
responseCallback | String or Function | The callback function that will be asynchronously invoked once the HTTP response is received. |
body | String | Optional: The content of the HTTP request (e.g., JSON, form data). Used mainly for methods like POST, PUT, or PATCH. |
headers | Array | Optional: Custom headers to include in the request. For example, ‘Authorization: Bearer |
thumbprint | String | Optional: If the web service requires a certificate, you can provide the certificate’s thumbprint as a Base64-encoded string for validation. |
caPath | String | Optional: The file path to the certificate, used if the web service requires a specific certificate for connection. |
Example
// Using a named callback function for GET request
HttpClient_SendRequest("https://foo.bar:443/api/json/example?reqid=12944", "GET", "HttpRequest_Callback");
// Using an anonymous callback function for DELETE request
HttpClient_SendRequest("https://foo.bar:443/api/json/example?reqid=12944", "DELETE", function (httpCode, responseHeaders, responseBody) {
// Handle response here
LogDebug("DELETE Response Code: " + httpCode);
LogDebug("DELETE Response Body: " + responseBody);
});
// Using POST method with form data and custom Content-Type header
HttpClient_SendRequest("https://foo.bar:443/api/json/example", "POST", HttpRequest_Callback, "firstname=john_doe&lastname=doe", ["Content-Type: application/x-www-form-urlencoded"]);
// Using PUT method with JSON data, custom Content-Type header, and a certificate thumbprint
HttpClient_SendRequest("https://foo.bar:443/api/json/example", "PUT", "HttpRequest_Callback", "{ 'id' : 12944, 'firstname' : 'john', 'lastname' : 'doe' }",
["Content-Type: application/json"], "sha256//oQ6yZTgZ5VdEojg5RmZbFLYYegM5h/YhdzMDXw==");
// Using PATCH method with JSON data, custom Content-Type header, and a certificate file path
HttpClient_SendRequest("https://foo.bar:443/api/json/example", "PATCH", HttpRequest_Callback,
"{ 'id' : 12944, 'firstname' : 'john' }", ["Content-Type: application/json"], "", "c:\\programdata\\foobar\\my_company.cer");
// The callback function to handle HTTP responses
function HttpRequest_Callback(httpCode, responseHeaders, responseBody) {
LogDebug("HTTP response code: " + String(httpCode));
LogDebug("HTTP response headers:");
for (var i = 0; i < responseHeaders.length; i++) {
LogDebug(responseHeaders[i]);
}
LogDebug("HTTP response body:");
LogDebug(responseBody);
}
Sample Project
Troubleshooting
Issue: Http response code is 0.
This result means there is something wrong with the http request. View the content of the response body to ascertain the cause of the error.