I'm trying to send the below JSON data request to the 3rd party system using the "N/https" modules https.post() method.
I'm getting the Response Code as "200" with Error Message "Unexpected character encountered while parsing value: S. Path '', line 0, position 0"
Please refer my code given below
var requestData = {
"terminal": "345678",
"user": "TestUser1234",
"password": "XXXXXX",
"Currency": "USD",
"Total": "25",
"GoodURL": "https://gatewayxx.test.com/sandbox/landingpage",
"Language": "EN"
};
log.debug('Typeof - RequestData: ', typeof requestData);
var headerObj = new Array();
headerObj['Content-Type'] = 'application/json';
headerObj['Accept'] = 'application/json';
var response = https.post({
url: "https://gatewayxx.test.com",
body: requestData
});
HTTPS POST Reponses Message:
{
"type": "http.ClientResponse",
"code": 200,
"headers": {
"Cache-Control": "private",
"Server": "Microsoft-IIS/7.5",
"Content-Length": "152",
"Date": "Fri, 02 Oct 2020 05:44:47 GMT",
"Content-Type": "text/html; charset=utf-8",
"Via": "1.1 mono002"
},
"body": "{\"URL\":\"\",\"ConfirmationKey\":\"\",\"Error\":{\"ErrCode\":599,\"ErrMsg\":\"Unexpected character encountered while parsing value: S. Path '', line 0, position 0.\"}}"
}
I check my request data in JSON validator, There is no error in it. Also in the code I have validated it using the typeof property. It is also showing it as "object".
Also if you notice the response message it is giving the response "Content-Type" as "text/html" instead of JSON data.
I'm not sure of what mistake I'm doing while sending the JSON data, can anyone help me to understand this issue.
Thanks a lot in advance.
You're specifying JSON data as the Content-Type in the headers, but you're passing a JavaScript Object in the body. You need to stringify the object to a JSON string:
var response = https.post({
url: "https://gatewayxx.test.com",
body: JSON.stringify(requestData)
});
Also, as pointed out by bluehank, you are not sending the headers with the request - you probably mean:
var response = https.post({
url: "https://gatewayxx.test.com",
body: JSON.stringify(requestData),
headers: headerObj
});