3

i am sending a GET request to a spring boot backend api, but i keep getting a 405 error, POST method not allowed

my code is

public class TextingSolution {


private final Contact xReceiver {get;set;}
public String MessageContent{get;set;}
public Boolean Success {get;set;}
// public integer reloadTime ;


public TextingSolution(ApexPages.StandardController controller){
    Id xId= System.currentPageReference().getParameters().get('Id');
    this.xReceiver = [Select Name, Phone From Contact Where Id=:xId];

}
public void ClosePopUp(){
    this.Success = false;

}
public void showPopUp(){
    this.Success = true;
}


public  PageReference SendMessage(){
    // perform Apex Callout Here
    String jsonBody = '{"phoneNumber":' +JSON.serialize(this.xReceiver.Phone) +', "text":'+ JSON.serialize(this.MessageContent)+'}';
    System.debug(jsonBody);
    PageReference page = ApexPages.currentPage();

    Http http = new Http();
    HttpRequest Request = new HttpRequest();

   //configure request
    Request.setEndpoint('http://3.19.59.9:8080/api/v1/sendMessage');
    Request.setHeader('Content-Type', 'application/json');
    Request.setBody(jsonBody);
    Request.setMethod('GET');

    system.debug(request);

    // send request, get response!
    HttpResponse Response = http.send(Request);
    System.debug('Response: ' +  Response.getBody());
    //if(Response.getStatusCode()==200){
      //  this.Success= true;
      //System.debug('Successful: '+ this.Success);
        page.setRedirect(true);

    //}else this.Success = false;

return  page;
}

}

but i get this response in the Debug

10:35:20:105 USER_DEBUG [43]|DEBUG|Response: <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Nov 18 09:35:20 UTC 2019</div><div>There was an unexpected error (type=Method Not Allowed, status=405).</div><div>Request method &#39;POST&#39; not supported</div></body></html>

somehow, the web api is seeing my request as a POST request. When i tested in PostMan with a get request, it worked, while with post request i got the error 405. any fixes ??

1
  • is it working via any rest client like postman? Nov 18, 2019 at 10:11

1 Answer 1

4

The problem is that you are trying to set a body on a GET request, which is not normal. There's some discussion on the subject of whether or not a body is allowed in a GET request, but the simple advice is not to do it.

any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

So, yes, you can send a body with GET, and no, it is never useful to do so.

From https://stackoverflow.com/questions/978061/http-get-with-request-body

If you remove the body from your code, you'll find SF sends it as a GET. Put it back in, and it gets converted to a POST.

Looking at your code, you're trying to set up an API to send a message. In which case, POST is probably the right HTTP verb anyway. So I'd change the spring end to expect and accept a POST.

If you really can't do that, you'll have to put your body into the URL as a request parameter.

2
  • 1
    In practice, I've observed that servers tend to see the body of a GET as a new request, and will end up throwing a 400 Bad Request error on the "second" request.
    – sfdcfox
    Nov 18, 2019 at 17:11
  • Thanks a lot. you were correct
    – Soji
    Jan 13, 2020 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.