1

I am running the below code in developer console to get OrgPercentage coverage, but it is throwing an error

"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'POST' not allowed. Allowed are HEAD,GET"

I am using "GET" method in my code. I am not sure where POST is happening

     HttpRequest reqOrgCoverage = new HttpRequest();
        String ClientId = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
        String ClientSecret = 'XXXXXXXXXXXXXXXXXXXXXXXX';
        String redirectUrl = 'test.salesforce.com';
        //reqOrgCoverage.setHeader('Authorization', 'OAuth' + UserInfo.getSessionID());
        reqOrgCoverage.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        reqOrgCoverage.setHeader('Content-Type', 'application/json');
        reqOrgCoverage.setBody('grant_type=authorization_code' +  
                    '&client_id=' + ClientId + 
                    '&client_secret=' + ClientSecret + 
                    '&redirect_uri='+redirectUrl);
        reqOrgCoverage.setEndpoint(URL.getSalesforceBaseURL().toExternalForm()+'/services/data/v43.0/tooling/query/?q=Select+PercentCovered+from+ApexOrgWideCoverage');
        reqOrgCoverage.setMethod('GET');
        Http h = new Http();
        HttpResponse resHttp = h.send(reqOrgCoverage);
 System.debug('response: ' + resHttp.getBody());
1

You don't need the following line of codes in your example.

reqOrgCoverage.setHeader('Content-Type', 'application/json');
reqOrgCoverage.setBody('grant_type=authorization_code' +  
                '&client_id=' + ClientId + 
                '&client_secret=' + ClientSecret + 
                '&redirect_uri='+redirectUrl);

Because you already have the authorization based on your session id, and that you have already set that in the header, you don't need to pass anything else.

Though you can set body with a GET requests, but setting body in a request signifies that you are POSTing data to the server. The GET method per RFC 2616 definition states that anything being sent should be part of the Request-URI.

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

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.