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.

3

There are 3 best solutions below

1
On BEST ANSWER

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
});
1
On

You seem to create a header for your request but in the end you dont use it in the post request. Any particular reason?

As you receive a 200 response code it means that the POST request went through correctly. However whatever you re trying to get is bugging. More specifically you get 599 error code, that is usually used for a Time out on most proxies. So the issue might not come from you but from the API you are using

1
On

I have found the NetSuite documentation wrong regarding the header object.

https.request() or await https.request.promise() uses a headers object like const headerObj = { 'Content-Type': 'application/json', 'Accept': 'application/json' };