The NetSuite https.post() method is throwing an error that reads "Encountered unexpected character while parsing value: S. Path '', line 0, position 0"

I'm currently facing an issue when trying to send the JSON data request below to a 3rd party system using the "N/https" modules https.post() method.

Upon sending the request, I receive a Response Code of "200" along with the Error Message "Unexpected character encountered while parsing value: S. Path '', line 0, position 0"

Here is my code snippet:

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 = {};
headerObj['Content-Type'] = 'application/json';
headerObj['Accept'] = 'application/json';

var response = https.post({
    url: "https://gatewayxx.test.com",
    body: requestData
});

HTTPS POST Responses 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.\"}}"
}

After validating my request data in a JSON validator and confirming its type as "object", I am puzzled by the mismatched content-type in the response message which indicated "text/html" rather than JSON data.

If anyone could provide insight into this issue, it would be greatly appreciated.

Thank you for your assistance.

Answer №1

When setting JSON data as the Content-Type in the headers, ensure that you stringify the JavaScript Object being passed in the body:

var response = https.post({
    url: "https://gatewayxx.test.com",
    body: JSON.stringify(requestData)
});

In addition, as mentioned by bluehank, remember to include the headers with the request like this:

var response = https.post({
    url: "https://gatewayxx.test.com",
    body: JSON.stringify(requestData),
    headers: headerObj
});

Answer №2

It appears that you have created a header for your request, but then failed to use it in the actual post request. Can you explain why this is?

Although you received a 200 response code indicating that the POST request was successful, there seems to be an issue with what you are trying to retrieve. You are encountering a 599 error code, typically associated with timeouts on proxies. This suggests that the problem may not be on your end, but rather with the API you are utilizing.

Answer №3

After close examination, I have discovered a discrepancy in the NetSuite documentation concerning the header object.

When using https.request() or await https.request.promise(), it is important to note that the headers object should be formatted as follows: const headerObj = { 'Content-Type': 'application/json', 'Accept': 'application/json' };

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging split and map functions within JSX code

const array = ['name', 'contact number'] const Application = () => ( <div style={styles}> Unable to display Add name & contact, encountering issues with splitting the array). </div> ); I'm facing difficul ...

jQuery can be used to mask phone numbers with three input fields

I am looking to implement phone field masking using jQuery or JavaScript. I have tried a few jQuery mask plugins, but I am still searching for a better solution. Specifically, I need three input fields for the USA (it is a requirement). The inputs needed ...

Utilizing JavaScript for form creation

Still learning the ropes of JavaScript and feeling a bit unsure about my skills. I'm trying to use JavaScript to create a new window with an input form within it. Managed to get a basic window set up with a dropdown menu, but struggling to implement ...

Save data to a JSON file within a Meteor application

I've been experimenting with running a small web scraper on Meteor using x-ray. My goal is to extract data and output it to a JSON file. While this setup works fine with node and express, I've run into issues when trying to get it working with Me ...

Decoding JSON Data on Android

I am relatively new to utilizing web services in Android development, and I am currently facing an issue with parsing JSON data from a URL. Below is my code snippet: JSONParser.java import java.io.BufferedReader; import java.io.IOException; import java.i ...

What is causing the lack of updated data on the components when navigating to a different page? (Vue.JS 2)

I am working with 2 components The first component looks like this : http://pastebin.com/M8Q3au0B Due to the long code, I have used pastebin for it The first component calls the second component The second component is as follows: <template> ...

The issue of `for` loop and `forEach` loop not functioning in EJS

I'm currently developing a To-Do App and facing difficulty with the POST request functionality. I've attempted using both a for loop and a forEach loop but none seem to be working. <% todos.forEach(item => { %> <li><%= item ...

Is there a way to dynamically calculate the total of a column when a new row is added using Javascript?

I am new to both javascript and cakephp. I successfully implemented a feature that allows me to add a new row using javascript, but now I am looking to calculate the total sum of the "amount" column whenever I input a value in the amount field. Below is th ...

Guide to profiling resources in Node.js applications

When developing Node.js applications, it's important to keep track of how your code is performing in terms of memory and IO. By monitoring these metrics, you can identify which parts of your code are causing delays or consuming excessive resources. Th ...

AlphaVantage Platform: Element not found

As someone new to API services, I'm currently working on constructing a dashboard that pulls data from the Alphavantage API. My goal is to retrieve data for 3 symbols simultaneously by creating a list and passing the index to my API call. Each symbol ...

The NodeJS application experiences a crash if incorrect parameters are provided to the API

Currently, I have built a CRUD API using TypeScript with Node.js, Express, and MongoDB. My goal is to ensure that the API functions correctly when the correct parameters are sent through a POST request. However, if incorrect parameters are passed, the Node ...

What steps should I take to ensure that jQuery functions remain functional on loaded code?

I have implemented modals on all my pages except for the first one to save time. Here is the script that loads my modals: $(function () { $('#header').load('reusenavbar.php'); $('#loginModal').load('reuseloginmo ...

Utilizing Rails for dynamic form validation with AJAX

As someone who is new to jQuery, AJAX, and JavaScript in general, I am facing a challenge with front-end validation for a Rails form that utilizes an ajax call to query the server. The validation works fine when I am debugging, giving enough time for the A ...

Tips for displaying a loading spinner each time the material table is loading

Hey there, I'm currently working on an Angular project where I need to display a table using Material Table. To indicate that the table is loading, I've defined a variable called isLoading. Here's how it works: In my TypeScript file: @Com ...

Unable to locate the element with the specified id: <path>

I am aiming to dynamically change the fill attribute of a specific <path> element when a page loads. Here is the detailed information about the path element: <path xmlns="http://www.w3.org/2000/svg" style="fill:#ff0000;stroke:#000000;stroke-wid ...

Utilizing local storage functionality within AngularJS

I've been encountering challenges while trying to integrate local storage using ngStorage in my ongoing AngularJS project. Despite fixing what seems to be the root cause of data type problems and errors during debugging, issues persist. Here is the P ...

Do you need to define a schema before querying data with Mongoose?

Is it necessary to adhere to a model with a schema before running any query? And how can one query a database collection without the schema, when referred by the collection name? This scenario is demonstrated in an example query from the Mongoose document ...

Struggling to customize checkbox design using HTML and CSS

Why am I unable to style HTML checkboxes with CSS? No matter what I try, the appearance of the checkboxes remains unchanged: Here is the HTML code: <input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /> <nput type="checkbox" id ...

Change the colors of a dynamic row in an HTML table using various options

I have successfully implemented a dynamic table using HTML and JavaScript. However, instead of applying alternate row colors, I want to assign a unique color to each row. How can I achieve this? <!DOCTYPE HTML> <html> <head> ...

Difficulty in accessing controller data in AngularJS with ng-repeat

I am trying to display comments using ng-repeat in a section, but I am having trouble accessing the data. Even after debugging, I cannot access the data without modifying the controller. I am new to Angular and prone to making mistakes. HTML / JS &apo ...