HttpClient_GetJson

This function is a simplified version of the HttpClient_SendRequest method, specifically designed for retrieving and deserializing JSON data. It automatically sets the appropriate HTTP method and headers required to fetch JSON from the specified URL endpoint. This method streamlines the process, making it easier to work with JSON responses.

Function

//ARGS:
//0: Url
//1: ResultCallback
//2: Headers [Optional]
//3: Thumbprint [Optional]
//4: CaCertPath [Optional]

void HttpClient_GetJson(url : string, responseCallback : string);
void HttpClient_GetJson(url : string, responseCallback : function);

void HttpClient_GetJson(url : string, responseCallback : string, headers : array);
void HttpClient_GetJson(url : string, responseCallback : function, headers : string array);

void HttpClient_GetJson(url : string, responseCallback : string, headers : array, thumbprint : string);
void HttpClient_GetJson(url : string, responseCallback : function, headers : string array, thumbprint : string);

void HttpClient_GetJson(url : string, responseCallback : string, headers : array, thumbprint : string, caPath : string);
void HttpClient_GetJson(url : string, responseCallback : function, headers : string 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.
responseCallback String or Function The callback function that will be asynchronously invoked once the HTTP response is received.
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_GetJson("https://foo.bar:443/api/json/example?reqid=12944", "HttpRequest_Callback");

// Including a custom Content-Type header
HttpClient_GetJson("https://foo.bar:443/api/json/example", function (code, headers, body) { LogDebug(String(code)); }, ["Authorization: Bearer <token>"]);

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

// Including a ca certificate path
HttpClient_GetJson("https://foo.bar:443/api/json/example", HttpRequest_Callback, [], "", "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:");
    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.