Skip to content

Developer Function: Server Script: httpClient.Post

Overview

Used when issuing a POST method using "httpClient" in "Server Script".

Syntax

httpClient.Post();

Parameters

There are no parameters.

Return Value

String-type return value

Usage Example

The following example issues a POST method to an external API server and logs the results.

JavaScript

MediaType: application/json example

let data = {
    data1: 'abc',
    data2: '123'
}
httpClient.RequestUri = 'https://servername/api/.....';
httpClient.Content = JSON.stringify(data);
let response = httpClient.Post();
if(httpClient.IsSuccess) {
    context.Log('Success: ' + response);
}else{
    context.Log('Error: (' + httpClient.StatusCode + ')' + response);
}

MediaType: application/x-www-form-urlencoded example

let data = {
    data1: 'abc',
    data2: '123'
}
httpClient.RequestUri = 'https://servername/api/.....';
httpClient.MediaType = 'application/x-www-form-urlencoded';
httpClient.Content = createParameters(data);
let response = httpClient.Post();
if(httpClient.IsSuccess) {
    context.Log('Success: ' + response);
}else{
    context.Log('Error: (' + httpClient.StatusCode + ')' + response);
}
//Function to convert the object to URL-encoded parameter format
function createParameters(obj) {
    let result =[];
    for(var key in obj) {
        result.push(encodeURIComponent(key) + '=' +encodeURIComponent(obj[key]));
    }
    return result.join('&');
}