"POST requires content-length error" occur when i am trying to call custom Rest API @HttpDelete Resource ..

HttpRequest  req1=new HttpRequest();
req1.setHeader('Authorization','OAuth '+access_token);

HttpResponse res=p1.send(req1);

global class ServerResource

  global static void doDelete() {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    String accountId=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    Account account = [SELECT Id FROM Account WHERE Id = :accountId];
    delete account;


POST requests require the Content-Length header to be set.

Salesforce does this automatically if you add a body to your HttpRequest. If you don't add a body then you need to manually set it to zero.

If you add the following to your callout you should be ok.

req.setHeader('Content-Length', '0');

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.