Loading

HTTP Callout fails with 'Content-Length is missing' error

Publiseringsdato: Oct 13, 2022
Beskrivelse
While invoking a HTTP Callout, 'Content-Length is missing' error is observed. 

For example if the HTTP Callout code is executed in Apex Anonymous, the below error is seen in the debug log:
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://xxxx');
req.setMethod('POST');
.......
........
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type','application/json');
HttpResponse res = h.send(req);

 Error:
{"fault":{"faultstring":"Content-Length is missing","detail":{"errorcode":"messaging.adaptors.http.flow.LengthRequired"}}}

 
Løsning
The content length header is missing.

If size of the entity body is known add it in the header. If the body is not present you can specify the length as 0.
req.setHeader('Content-Length', '0');

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

HttpRequest Class
List of HTTP header fields
Knowledge-artikkelnummer

000387575

 
Laster
Salesforce Help | Article