Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP cURL request fails with 411 Length Required

Error411 Length Required

Short answer

A 411 means the server refuses a request that does not state its body length. In PHP cURL it happens when you POST with no body at all, or when the request goes out with chunked transfer encoding. Send a Content-Length header (0 for an empty body), or remove the Transfer-Encoding header and give cURL a complete string body.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the request headers cURL actually sent. Look for a missing Content-Length line or a Transfer-Encoding: chunked line.

    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $response = curl_exec($ch);
    echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
  2. 2Match what you see, then jump to its fix

    1. 1The sent headers show POST but no Content-Length line, for an endpoint that takes no payload, such as an action URLA POST (or PUT) is sent with no body and no Content-LengthFix
    2. 2The sent headers contain Transfer-Encoding: chunked, and the server answers 411The request is sent with chunked transfer encodingFix

What does the error look like?

  • 411 Length Required
  • HTTP/1.1 411 Length Required

Cause 1 of 2

A POST (or PUT) is sent with no body and no Content-Length

1Check

A request with no content has nothing to measure, so some servers and proxies insist on an explicit zero. RFC 9110 says a POST with empty content still sends the header, as 0.

grep -n 'CURLOPT_POST\|CURLOPT_POSTFIELDS' src/CalendarClient.php

2Fix

Add the header yourself with CURLOPT_HTTPHEADER and give an empty string body.

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $token,
    'Content-Length: 0',
]);

3Verify

Run the call again; the status should no longer be 411.

echo curl_getinfo($ch, CURLINFO_HTTP_CODE);

Confirmed by rfc-editor.org

Did this fix it?

Cause 2 of 2

The request is sent with chunked transfer encoding

1Check

cURL sends the body in chunks when the request carries a Transfer-Encoding: chunked header. A server that wants a defined length answers 411, and RFC 9110 says the client may retry with a valid Content-Length.

grep -rn 'Transfer-Encoding' src/

2Fix

Remove that header (a header with an empty value after the colon removes it in cURL) and pass the whole body as one string, so the length is known.

$body = json_encode($event);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Transfer-Encoding:',
    'Content-Length: ' . strlen($body),
]);

3Verify

The sent headers show Content-Length and no Transfer-Encoding.

echo curl_getinfo($ch, CURLINFO_HEADER_OUT);

Confirmed by curl.se

Did this fix it?

How do I stop it coming back?

  • Print CURLINFO_HEADER_OUT in your debug mode so the real request is visible.
  • Build JSON bodies as a string first, then compute the length from that same string.
  • Replace a hand-written header list in one place; calling CURLOPT_HTTPHEADER twice replaces the first list.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.