- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 10:57 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 12:56 PM
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 11:04 AM
I see this at times if the API I am calling want's things URLEncoded within string values. Description on ServiceNow is one of those that can be an issue with ' " \r \n \t, etc.
Thanks
Aoife
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 11:15 AM
Thanks Aoife. I don't think that is the issue.
The body we are passing is just simple JSON, and in this case has no special characters, etc. It is simply: { "action": "get_settings" } Also, this is the body of the request, not the url.
In PostMan, passing { "action": "get_settings" } as the body works, and we receive the expected result. Passing the same body via SN results in the error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 12:16 PM
Understand that it is the body, still have to URLEncode strings at times.
Is you REST Outbound Message set properly to send the Content-Type: application/json
Aoife
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 12:25 PM
Yes, the content-type header is set. I just found this in the ServiceNow documentation for setRequestBody():
"Sets the body content to send to the web service provider when using PUT or POST HTTP methods."
So I believe setting the body for RESTMessageV2 only works for PUT and POST. Hoping someone can confirm this so I can let the customer know. Also, start looking for alternatives.