HttpClient_SendJson

This method is a simplified version of HttpClient_SendRequest, designed specifically for submitting JSON data to a specified web service. It automatically sets the appropriate HTTP method and headers required to send and receive JSON from the specified URL endpoint. This streamlined approach makes working with JSON more convenient and eliminates the need to manually configure HTTP methods or headers.

Function

// ARGS:
// 0: Url
// 1: Body
// 2: ResultCallback
// 3: Method [Optional] ("POST", "PUT", "PATCH", "DELETE")
// 4: Headers [Optional]
// 5: Thumbprint [Optional]
// 6: CaCertPath [Optional]

void HttpClient_SendJson(url : string, body : string, responseCallback : string);
void HttpClient_SendJson(url : string, body : object, responseCallback : function);

void HttpClient_SendJson(url : string, body : string, responseCallback : string, method : string);
void HttpClient_SendJson(url : string, body : string, responseCallback : function, method : string);

void HttpClient_SendJson(url : string, body : string, responseCallback : string, method : string, headers : array);
void HttpClient_SendJson(url : string, body : string, responseCallback : function, method : string, headers : array);

void HttpClient_SendJson(url : string, body : string, responseCallback : string, method : string, headers : array, thumbprint : string);
void HttpClient_SendJson(url : string, body : string, responseCallback : function, method : string, headers : array, thumbprint : string);

void HttpClient_SendJson(url : string, body : string, responseCallback : string, method : string, headers : array, thumbprint : string, caPath : string);
void HttpClient_SendJson(url : string, body : string, responseCallback : function, method : string, headers : array, thumbprint : string, caPath : string);

void HttpRequest_Callback(httpCode : int, httpHeaders : array, responseObject : object);

Parameters

Name Type Description
url String The URL endpoint to which the HTTP request will be sent.
body String or Object The content of the HTTP request as a JSON string, or JavaScript object to be serialised.
responseCallback String or Function The callback function that will be asynchronously invoked once the HTTP response is received.
method String Optional: The HTTP method to use for the request. Supported methods: ‘POST’, ‘PUT’, ‘DELETE’, ‘PATCH’. Default = ‘POST’
headers Array Optional: Custom headers to include in the request. For example, ‘Authorization: Bearer ’. Default headers (Content-Type: application/json; and Content-Length: <body.size()>) will be added automatically.
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 request
HttpClient_SendJson("https://foo.bar:443/api/json/example?reqid=12944", "{ 'id' : 12944, 'firstname' : 'john', 'lastname' : 'doe' }", "HttpRequest_Callback");

// Using a JavaScript object instead of a JSON string 
var user = { id: 12944, firstname: "john", lastname: "joe" };
HttpClient_SendJson("https://foo.bar:443/api/json/example?reqid=12944", user, function (code, headers, body) { LogDebug(String(code)); });

// Sending the request as a PUT instead of the default POST
HttpClient_SendJson("https://foo.bar:443/api/json/example", user, "HttpRequest_Callback", "PUT");

// Including a custom Content-Type header
HttpClient_SendJson("https://foo.bar:443/api/json/example", user, HttpRequest_Callback, "POST", ["Authorization: Bearer <token>"]);

// Including a certificate thumbprint
HttpClient_SendJson("https://foo.bar:443/api/json/example", user, HttpRequest_Callback, "POST", [], "sha256//oQ6yZTgZ5VdEojg5RmZbFLYYegM5h/YhdzMDXw==");

// Including a CA certificate path
HttpClient_SendJson("https://foo.bar:443/api/json/example", user, HttpRequest_Callback, "POST", [], "", "c:\\programdata\\foobar\\my_company.cer");

// The callback function to handle HTTP responses, with the response body already deserialized
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 (if applicable):");
    LogDebug(responseBody.property1);
    LogDebug(responseBody.property2);
    LogDebug(responseBody.property3);
}

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.