logo

NJP

Set Request body of POST method in REST Message

Import · May 03, 2021 · article

1. Create a string and set all the variables to be passed in request body as the key along with value separated by an '&' as below:

var body = "grant_type=credentials&resource=https://xyz.com&type=encoded&key=pqr1t5h8&user=jhonson";

2. Create a REST Message and use the content type as "application/x-www-form-urlencoded" in the request header:

var r = new sn_ws.RESTMessageV2(); r.setEndpoint("https://abc.com"); r.setRequestHeader("Accept", "application/json"); r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ******important***** r.setRequestBody(body); r.setHttpMethod('POST'); var response = r.execute(); var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

View original source

https://www.servicenow.com/community/developer-articles/set-request-body-of-post-method-in-rest-message/ta-p/2330013