Outbound REST - GET request with body: Error

Justin Lee2
Tera Guru

We are building a custom app for a customer, and are connecting to their REST API.  They have a call that is a GET method, but also has a body.

Our simplified code is:

var api = new sn_ws.RESTMessageV2("SOME_API", "SOME_GET_METHOD");
api.setRequestBody(JSON.stringify({ property: "something"}));
var response = api.execute();

The response is a 400, but we have outbound logging enabled, and the full response is:

{"result":"Failure","error":"Request data is malformed and cannot be decoded"}

We make the same call in PostMan (an online REST API tester), and it is successful. If we clear the body of the request in PostMan, we receive the same response (error) as we do in the ServiceNow call. So it seems that RESTMessageV2 is not passing the body in the request.

Does sn_ws.RESTMessageV2 ignore setRequestBody for GET methods?  If so, what are the alternatives?

 

1 ACCEPTED SOLUTION

I can't believe I didn't catch that.  That's correct, an HTTP GET has no body, it has no meaning on a GET operation for the HTTP protocol.  You are GETting a body, not POSTing one or PUTting one.

ServiceNow often takes a hard line on such things and even though many have create REST APIs that have a body on a GET request, since it is non-standard, ServiceNow does not support it.

It may not be possible with ServiceNow REST API methods.

Per Roy Tutorials on REST

Yes, you can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in https://tools.ietf.org/html/rfc2616#section-4.3.

Aoife

View solution in original post

7 REPLIES 7

I can't believe I didn't catch that.  That's correct, an HTTP GET has no body, it has no meaning on a GET operation for the HTTP protocol.  You are GETting a body, not POSTing one or PUTting one.

ServiceNow often takes a hard line on such things and even though many have create REST APIs that have a body on a GET request, since it is non-standard, ServiceNow does not support it.

It may not be possible with ServiceNow REST API methods.

Per Roy Tutorials on REST

Yes, you can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in https://tools.ietf.org/html/rfc2616#section-4.3.

Aoife

MichaelZischeck
Kilo Sage

did you ever solve this? I have the same issue... a non-standard REST API which requires a body for GET

No - there is no work around that I'm aware of.  We reached out to the API developer and had them change their API to a PUT.