Loading

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

Udgivelsesdato: Jun 19, 2026
Beskrivelse

When invoking an HTTP POST callout from Salesforce Apex, a 'Content-Length is missing' error may appear in the debug log. This error is observed when making HTTP POST requests using the Apex HttpRequest class.

Example scenario: An HTTP POST callout is executed in Apex using the HttpRequest class. The request sets an endpoint, method, Authorization header, and Content-Type header. However, the Content-Length header is not set. 
The error code messaging.adaptors.http.flow.LengthRequired indicates that the HTTP POST request is missing the required Content-Length header.

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);


When the request is sent, the following error is returned by the server:

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

The error code messaging.adaptors.http.flow.LengthRequired indicates that the HTTP POST request is missing the required Content-Length header.

 

Løsning

The error occurs because HTTP POST requests require a Content-Length header to indicate the size of the request body.

Fix: Set the Content-Length Header

If the request body is empty, set the Content-Length value to 0 using the following Apex method on your HttpRequest object:
req.setHeader('Content-Length', '0');
If the request body has content, calculate the byte length of the body string and pass it as the Content-Length value.

Why This Is Required

POST requests by the HTTP specification require the Content-Length header when sending a body. If the body is absent or the header is missing, many servers will reject the request with a 411 Length Required error — which is the error code shown above (LengthRequired).



Vidensartikelnummer

000387575

 
Indlæser
Salesforce Help | Article