+ Start a Discussion
Ashmi PatelAshmi Patel 

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

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


i m getting this error while using apex REST services
i m getting this in workbench
Agustina GarciaAgustina Garcia
Could you give us more information about it? 

Have you created the Rest class ? Please, share the code.

Are you just making a REST call? Could you share a screenshot of your call in Workbench? Maybet there is something missing.

 
Ashmi PatelAshmi Patel
This is my apex class::::

global class LeadInfoParser {
    global String LastName{get;set;}
    global String FirstName{get;set;}
    global String Company{get;set;}
    global String email{get;set;}
    global String Phone{get;set;}
    global String LeadStatus{get;set;}    

}

This is REST service

@RestResource(urlMapping='/LeadService/*')
global without sharing class LeadService {
    @HttpPost
    global static String createLead(LeadInfoParser leadrec){
        Lead leadObj = new Lead();
        leadObj.FirstName =leadrec.FirstName;
        leadObj.LastName=leadrec.LastName; 
        leadObj.Phone=leadrec.Phone;
        leadObj.email=leadrec.email; 
        leadObj.Company = leadrec.Company;
        leadObj.Status = leadrec.LeadStatus;             
      
        Database.saveResult saveResult = database.insert(leadObj,false);
        if(saveResult.isSuccess()){
            System.debug('Record Id:'+saveResult.getId());
        }
        else{
            System.debug('saveResult:'+saveResult.getErrors());
        }
        //Response
        JSONGenerator gen=JSON.createGenerator(true);     
        gen.writeStartObject();
        gen.writeStringField('message','Lead record is created Successfully');
        gen.writeEndObject();     
        String responseString= gen.getAsString();
        return responseString;
    }

}
Agustina GarciaAgustina Garcia
Please, share also your call in workbench.
Ashmi PatelAshmi Patel

User-added image

this is Request Body:
{"leadrec":{
   "FirstName":"Laxman",
   "LastName":"Vattam",
   "Company":"Google",
   "LeadStatus":"Open",
   "email":"laxmanVattam@gmail.com",
   "Phone":"+1123456"

 }
Agustina GarciaAgustina Garcia
It seems you are missing the call to the method, try to append at the end the createLead. Let me know if it doesn't work and I will check the code.
Ashmi PatelAshmi Patel
i cant get ...plz help
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati

Ashmi, Change the URL to the below specified : 

/services/apexrest/LeadService 
 

Agustina GarciaAgustina Garcia
Yes, exactly above url works. Check also the JSON file as there is a } missing
 
{"leadrec":{
   "FirstName":"Laxman",
   "LastName":"Vattam",
   "Company":"Google",
   "LeadStatus":"Open",
   "email":"laxmanVattam@gmail.com",
   "Phone":"+1123456"

 }
}

Also, would like to explain that you can provide a url with a different name from the class. For instance this also works:
 
@RestResource(urlMapping='/creations')
global without sharing class LeadService {
    @HttpPost
    global static String createLead(LeadInfoParser leadrec)
    {
        //your code
    }
}

And make this call on Workbench
 
/services/apexrest/creations/

 
ved16sepved16sep
Please help me..

My apexrest class is working fine but how do we call it using java,Its need oauth. I have tried to call it using termial but it giving two errors..
1. errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'GET' not allowed. Allowed are POST".
2. "message" : "Session expired or invalid",
  "errorCode" : "INVALID_SESSION_ID"

I have used following command:-
root@arosysus02:/# curl --form client_id=3MVG9d8..z.hDcPK7fgUsuAPlGUr4RGiRVJZIpUhLazMe195janwSs5PfTtySi25gajFxyYOJvU14dvOA9F6h --form client_secret=713746856626476078 --form grant_type=password --form username=ved16sep@arosys.com --form password=Tech4ArosyssCcC2eGIog2PYpSXG7rl2qefI -k https://ap5.salesforce.com/services/oauth2/token
{"access_token":"00D7F0000002eIJ!ARgAQIhVRYEXdUu3ZjQt0iw1QSjtn3ot5fRcVe4tev.HbdC58m91z22zfOQKgAp_Ydbvbqh.U2EMVxaZ8CV2INSzI9Z_JxNp","instance_url":"https://ap5.salesforce.com","id":"https://login.salesforce.com/id/00D7F0000002eIJUAY/0057F000000lEHDQA2","token_type":"Bearer","issued_at":"1504602418050","signature":"BMpJ3Gn8RDn/ex9hujn08T174cwAPooQafvUyxoeUME="}root@arosysus02:/# curl https://ap5.salesforce.com/services/apexrest/Fmcn/MyService/Lead -H "Authorization: Bearer https://login.salesforce.com/id/00D7F0000002eIJUAY/0057F000000lEHDQA2" -H "Content-Type: application/json" -H 'X-PrettyPrint:1'
[ {
  "message" : "Session expired or invalid",
  "errorCode" : "INVALID_SESSION_ID"
} ]

May be I am missing something...

Thanks,
-Ved