function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Erik YearyErik Yeary 

404 Cannot POST

I'm doing an API Callout to an external REST endpoint from Apex. The endpoint is configured for GET and my code reflects that. For some reason, I am getting a "404: Not Found: Cannot POST /cancelAppointment". I'm confused because my request is clearly designated as a GET.

The site is correctly added to my remote site settings. Also - if I run the same request from Insomnia/Postman, it works fine. 

My code:

Http http = new Http();
  
HttpRequest request = new HttpRequest();
request.setEndpoint('https://ourwebsite.com/cancelAppointment');
request.setTimeout(20000);
request.setMethod('GET');
request.setHeader('Content-Type', 'text');
request.setHeader('Authorization', 'JWT ' + token); //token is a JWT 
request.setBody(getJSONBody()); //this is confirmed to work correctly
        
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) 
{
        System.debug(response.getBody());
}
else
{
System.debug(response.getStatusCode() + ': '  + response.getStatus();
System.debug(response.getBody());
}
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/260695/apex-call-out-404-not-found-error-on-developer-console-while-it-works-perfectly

This might help you.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
iRKiRK
Hi,

Could it be that the web server that you are calling is interpreting the method as POST because you are passing a body. GET calls don't usually contain body at all but everything necessary is included in the query string in the URL.

Regards,
iRK