5

I have exposed a Rest service method to public

@RestResource(urlMapping='/webhook')
global class Services {
    Private static String ACTION_PARAM =  'action';
    Private static String FETCH_UPDATES =  'fetchUpdates';

    @HttpPost
    global static void  handleRequest() {
        RestResponse response = RestContext.response;
        try{

            response.addHeader('Content-Type','application/json');
            RestRequest req = RestContext.request;
            String action = req.params.get(ACTION_PARAM);
            Object result;
            if(FETCH_UPDATES.equalsIgnoreCase(action)){
                result = ServicesHandler.fetchUpdates(req.requestBody.toString());
            }
            response.responseBody = Blob.valueOf(String.valueOf(result));
        }catch(Exception ex){
            response.responseBody = Blob.valueOf(ex.getMessage());
        }

    }
}

Here fetchUpdate method do some callout to an external system Like below request

enter image description here

When I do this request from Postman, it gives expected result but from apex it gives below response

System.HttpResponse[Status=Method Not Allowed, StatusCode=405]

So My questions are :

  1. Does site guest user is not allowed to make any external service callout?
  2. My classes are running without with sharing, so they should run in god mode, so why they are not getting result.
  3. AM I doing something wrong?

Please let me know if anyone need more information to add some more context.

Please help me

  • Do you have a Remote Site Setting configured in this org allowing callouts to this URL? – Mark Pond Jan 6 '16 at 23:11
  • @MarkPond That would have created a CalloutException. – sfdcfox Jan 7 '16 at 0:35
  • Does changing HttpPost to HttpGet on handleRequest() fix the issue? – Ryan Guest Jan 7 '16 at 0:51
1

"Method Not Allowed" is a specific HTTP error that means you've used the wrong method. It has nothing to do with authentication, an invalid endpoint, etc. It simply means you're trying to do something you haven't allowed (e.g. you're trying to perform a GET instead of a POST).

1

Answers:

  1. I've made callouts back to Salesforce itself (which requires remote site settings to be made) under the site guest user so I doubt that is the problem.
  2. The "god mode" idea relates to access rights to Salesforce data not external web services so is not relevant.
  3. So the first place I would look is the implementation of ServicesHandler.fetchUpdates, confirming that the endpoint is identical to the one that works in your manual test, that the right method is being used, and that the Basic Authentication is correctly constructed and set in the right header.

Just tried this callout code from a site and it worked fine so item 1 isn't the problem:

@RestResource(urlMapping='/proxy')
global class Proxy {
    @HttpGet
    global static String get() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://www.google.com');
        req.setMethod('GET');
        return new Http().send(req).getBody();
    }
}
  • 405 means the wrong "http verb" has been used for the service, for example, using POST / HTTP/1.1 when the path doesn't support POST. – sfdcfox Jan 7 '16 at 0:34
  • @sfdcfox Because I was doing a GET request and was also setting Body for the request like request.setBody(data); . When I commented this, it works. – ashishcloud Jan 7 '16 at 6:30
  • Below paste snippet is my Java Rest implementation pastebin.com/embed_js/9BhduvSQ – ashishcloud Jan 7 '16 at 6:33
  • 1
    @SFDCGOD A little known tidbit: GET doesn't support a body. If you set the body, this overrides the method from GET to POST. – sfdcfox Jan 7 '16 at 7:11
  • 1
    @SFDCGOD It doesn't matter how it debugs, it matters how the server actually sends it. The HttpRequest object is only the requested configuration. The code that actually opens the connection will adjust your request accordingly. I can probably write up a demo to show it in action. – sfdcfox Jan 7 '16 at 10:11

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.