0

How do you do a HttpRequest for a GET method with a request body? For some reason this GET method is getting converted to POST and is not successful. This is working via postman.

 Http http = new Http();
 HttpRequest request = new HttpRequest();
 HttpResponse response;

 request.setEndpoint('http://www.someendpoint.com/getdata');
 request.setMethod('GET');

 String requestBody = '{ "accountId": "123345" }';

 request.setBody(requestBody);
 response = http.send(request);

We are getting the following

 {
    "errors" : [ {
       "message" : "Request method 'POST' not supported",
       "type" : "HttpRequestMethodNotSupportedError"
    } ]
 }
5
  • Have you tried setting the method as post instead of get?
    – Raul
    Commented Mar 8, 2022 at 22:53
  • Yes. "request.setMethod('POST');" and still the same issue.
    – jojopogi
    Commented Mar 8, 2022 at 22:56
  • Might be better to check with the owner of endpoint on what would be a right way to make this call? Does this third party has a documentation and mentions on how you can make the callout?
    – Raul
    Commented Mar 8, 2022 at 23:01
  • Yes, it's really just to GET some information and the input parameters are set in the body. And yes, we checked with the owner of the endpoint.
    – jojopogi
    Commented Mar 8, 2022 at 23:17
  • did you mean String requestBody = '{ "accountId": "123345" }'; ?
    – cropredy
    Commented Mar 9, 2022 at 0:34

1 Answer 1

2

Some info about GET method with body Http GET methods with body are kind of grey area. Per spec you can include body in GET and server must parse it but they are not supposed to use it to serve GET.

See this thread for more info.

About your question Salesforce is upgrading the method to POST because you included request body. If you comment out code where you are setting body, sfdc leaves the method as GET. So either you need to work with server to support POST for this endpoint or support query params for data you are posting.

This is working in Postman because it is not upgrading the method like sfdc.

Btw, httpbin.org is an excellent resource to test what sfdc is sending to server.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .