top of page

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,080 per month

ServiceNow REST API Integrations: Sending JSON Payloads Correctly




Integrating ServiceNow with external systems often involves sending data via REST APIs. When using the POST method, it's common to send data in JSON format. However, ServiceNow developers sometimes encounter challenges in correctly formatting the request body as JSON when using the RESTMessageV2 API. This article will guide you through the proper way to set the request body as JSON in your ServiceNow scripts, ensuring seamless communication with external services.


The Challenge: Sending JSON Data via RESTMessageV2 POST


Imagine you're building a ServiceNow application that needs to send data to a third-party service. You've chosen to use the RESTMessageV2 API and the POST method, and the target service expects the data in JSON format. You might construct your JSON payload as a string or a JavaScript object and then attempt to set it as the request body. However, you might find that while your ServiceNow script executes without errors and you even receive a successful HTTP 200 response, the receiving service isn't correctly interpreting the data you sent. This often happens because the request body isn't being formatted as a proper JSON string.


The Solution: Utilizing JSON.stringify() for Correct JSON Formatting


The key to correctly setting the request body as JSON in ServiceNow using RESTMessageV2 is to ensure that your data is a properly formatted JSON string. ServiceNow's JavaScript engine provides the JSON.stringify() method specifically for this purpose. This method converts a JavaScript object into its corresponding JSON string representation.


Step-by-Step Guide to Setting JSON Request Body:

  1. Instantiate RESTMessageV2: Begin by creating a new instance of the sn_ws.RESTMessageV2 object.

var rm = new sn_ws.RESTMessageV2();
  1. Set Endpoint and HTTP Method: Specify the endpoint URL of the target service and set the HTTP method to 'POST'.

rm.setEndpoint('YOUR_REST_API_ENDPOINT_URL'); rm.setHttpMethod('POST');
  1. Configure Authentication (if required): If the target API requires authentication, configure it using methods like setBasicAuth().

rm.setBasicAuth('YOUR_USERNAME', 'YOUR_PASSWORD');
  1. Set the Content-Type Header: Inform the target service that you are sending JSON data by setting the Content-Type header to application/json.

rm.setRequestHeader('Content-Type', 'application/json');
  1. Create Your JSON Payload as a JavaScript Object: Construct your data as a JavaScript object with key-value pairs.

var payloadObject = {
	"input": {
		"context": {
			"system": {
				"dialog_request_counter": 170,
				"dialog_stack": {
					"dialog_node": ""
				},
				"dialog_turn_counter": 170
			}
		}, "text": "Your Input Data"
	}
};
  1. Convert the JavaScript Object to a JSON String: Use the JSON.stringify() method to convert your JavaScript object into a JSON string.

var jsonPayload = JSON.stringify(payloadObject);
  1. Set the Request Body: Use the setRequestBody() method of the RESTMessageV2 object to set the JSON string as the request body.

rm.setRequestBody(jsonPayload);
  1. Execute the Request: Finally, execute the REST API call.

var response = rm.execute();
  1. Handle the Response: Process the response, including retrieving the response body and status code.

var responseBody = response.getBody(); 
var httpStatus = response.getStatusCode();

Practical Example:

Let's say you want to send a simple message to an external service. You would first create a JavaScript object:

var messageObject = {
    "message": "Hello from ServiceNow!"
};

Then, convert it to a JSON string:

var messageJSON = JSON.stringify(messageObject);

Finally, set this JSON string as the request body using rm.setRequestBody(messageJSON);.


Relevant Use Cases:

This method is crucial in various ServiceNow integration scenarios, such as:

  • Sending incident or problem details to external monitoring systems.

  • Creating or updating records in third-party applications.

  • Interacting with AI or machine learning platforms that consume JSON data.

  • Pushing notifications or alerts to external communication channels.


Alternative Solutions:

While directly using JSON.stringify() with setRequestBody() is the most straightforward and recommended approach for sending JSON data, you might encounter scenarios where you are constructing the JSON string manually. In such cases, ensure that the string is valid JSON. However, using JSON.stringify() from a JavaScript object is generally less error-prone.


Conclusion: Ensuring Successful JSON Data Transmission


Successfully integrating ServiceNow with external APIs that expect JSON data hinges on correctly formatting the request body. By consistently using the JSON.stringify() method to convert your JavaScript objects into JSON strings before setting them as the request body in your RESTMessageV2 calls, you can avoid common pitfalls and ensure that the receiving services can properly interpret the data you are sending. Remember to always set the Content-Type header to application/json to clearly indicate the format of your request. This practice will lead to more robust and reliable ServiceNow integrations.

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,080 per month

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page