0

So i am trying to insert some account record. Token is right because get command works well. Error while Post - POST requires content-length. I am even trying to add body with JSONGenerator.

     Http h1 = new Http();
     HttpRequest req1 = new HttpRequest();
     req1.setHeader('Authorization','Bearer '+accesstoken);
     req1.setHeader('Content-Type','application/json');
     req1.setHeader('accept','application/json');
     JSONGenerator gen = JSON.createGenerator(true);
     gen.writeStartObject();
     gen.writeObjectField('name', 'Test Account');
     gen.writeEndObject();
     String body = gen.getAsString();
     request.setBody(body);
     req1.setMethod('POST');    
     req1.setEndpoint('url/services/data/v52.0/sobjects/account');
     HttpResponse res1 = h1.send(req1);
     system.debug('RESPONSE_BODY'+res1.getbody());
1

1 Answer 1

0

You have no body in req1, because you have set it to request.setBody(body);. You need to change this line to req1.setBody(body); and content-length will appear automatically on request with body. For POST it is required.

1
  • Oh, thanks. Сouldn't see if for 30 minutes)
    – gignecisto
    Aug 17, 2021 at 7:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .