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
Stéphane CStéphane C 

HTTP Callout method "405 Method Not Allowed"

Hi !

I'am trying to make a callout to a remote site api. This api is called with the url https://api.insee.fr/entreprises/sirene/V3/siren and the method GET.

I played with this api using RESTer and POSTMAN and it's worked fine. When I implemented it with APEX, the call systematically returns 405 Method Not Allowed.  I'm using the right URL and method but it keeps this answer. The site is allowed as a remote site.

Here is some APEX :
 
public static HTTPResponse HttpCall(String endpoint, String protocol, Map<String, String> mapHeaders, String strBody, Integer timeout)
    {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod(protocol);

        	if(timeout!= null)
                req.setTimeout(timeout); 
            
            if(mapHeaders != null)
                for(String headerkey : mapHeaders.keyset())
                    req.setHeader(headerkey, mapHeaders.get(headerkey));        
            
            if(strBody != null)
                req.setBody(strBody);
            
            system.debug(req);
            Http http = new Http();
            return http.send(req);
    }

    public static void queryBySiren()
    {        
        system.debug('QUERY BY SIREN:');
		String endpoint = 'https://api.insee.fr/entreprises/sirene/V3/siren';
		String protocol = 'GET';
		Map<String, String> headers = new Map<String, String>();
        String body = '';
        Integer timeout = 50000;
        HTTPResponse resp = SireneRequest.HttpCall(endpoint, protocol, headers, body, timeout);
        
        system.assert('OK' == resp.getStatus(), 'Request error : ' + resp.getstatusCode() + ' ' + resp.getStatus());
    }
Because it's working on RESTer and POSTMAN i'am a litle bit confused.

PS : Instead of returning 405 the api should return 401. Obviously i didn't enter credentials for some reasons ;)
Best Answer chosen by Stéphane C
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi Stéphane C,

Made few corrections and this should work.
 
public static HTTPResponse HttpCall(String endpoint, String protocol, Map<String, String> mapHeaders, String strBody, Integer timeout)
    {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod(protocol);

        	if(timeout!= null)
                req.setTimeout(timeout); 
            
            if(mapHeaders != null)
                for(String headerkey : mapHeaders.keyset())
                    req.setHeader(headerkey, mapHeaders.get(headerkey));        
            
            if(strBody != null)
                req.setBody(strBody);
            
            system.debug(req);
            Http http = new Http();
            HTTPResponse resp =  http.send(req);
            return resp; // for body use return resp.getBody(); 
    }


Hope this helps.If its solves, please mark it as best answer, so that others can benefit.

Regards,

Santosh.

All Answers

Stéphane CStéphane C
I'd like to be more precise : you can try querying with RESTer/POSTMAN, the url and the method will return 401 which is the right return (except when i add credentials). But when I use the given code, it's return 405 whenever i use credentials or not.
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi Stéphane C,

Made few corrections and this should work.
 
public static HTTPResponse HttpCall(String endpoint, String protocol, Map<String, String> mapHeaders, String strBody, Integer timeout)
    {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod(protocol);

        	if(timeout!= null)
                req.setTimeout(timeout); 
            
            if(mapHeaders != null)
                for(String headerkey : mapHeaders.keyset())
                    req.setHeader(headerkey, mapHeaders.get(headerkey));        
            
            if(strBody != null)
                req.setBody(strBody);
            
            system.debug(req);
            Http http = new Http();
            HTTPResponse resp =  http.send(req);
            return resp; // for body use return resp.getBody(); 
    }


Hope this helps.If its solves, please mark it as best answer, so that others can benefit.

Regards,

Santosh.

This was selected as the best answer
Anurag DandamudiAnurag Dandamudi
Hi Stephane,

How did yu resolve this?

Thanks,
Anurag