How can i get the length of a curl Post body

curl post file
curl post json file
php curl post
curl specify get or post
curl post "-d" option
curl post header
curl post xml
curl content-length

I am trying to log in to a remote website which requires me to post username password and login via post. I am using the Curl Option

curl_setopt($ch, CURLOPT_POSTFIELDS , $postData);

and assign my info like this

$postData['email'] = '';
$postData['password'] = 'Secret';

But the remote server complains about

HTTP Error 411. The request must be chunked or have a content length.

So how can i tell the content length of my postData ?

To get the length of PostData, you can use PHP's function count to get it.


#Get the length
$postData_len = count($postData);

#Set Curl Header with Content Length
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: '.$postData_len));

The above code with calculate the length of the postData array and then set the Curl header, if you're looking for 1 line of code, try this:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: '.count($postData)));

HTTP POST, When talking to a HTTP 1.1 server, you can tell curl to send the request body without a Content-Length: header upfront that specifies exactly how big the POST is  The Content-Length indicates the size of the response. Here the response size is 23 bytes. POST request with cURL The application has a POST endpoint /test.

Not sure this will work for you or not? but you need something like this with strlen() to set Content-Length.

$postData= array("email" => "", "password" => "Secret","Submit"=>"Login");                                                                    
$data_string = json_encode($postData);                                                                                   

$ch = curl_init('');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       

$result = curl_exec($ch);

curl POST examples · GitHub, For sending data with POST and PUT requests, these are common curl options: var bodyParser = require('body-parser'); is immensely useful if you don't know the length, (whereas --data and --data @file will read the entire contents of the  A POST with no Content-Length and no Transfer-Encoding is perfectly legal and has no message body. Adding -d "" causes CURL to send a Content-Length: 0 header, which resolves the problem. You could modify lighttp. Find the code that issues the 411 error and instead set the content length to zero.

$ch = curl_init();
$data['email'] = '';
$data['password'] = 'Secret';
curl_setopt($ch, CURLOPT_URL, '****/upload.php');

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


the header of the request is


[CONTENT_TYPE] => multipart/form-data; boundary=------------------------aac448045c2fe517

however if we pass the args through string like this

$ch = curl_init();
$data['email'] = '';
$data['password'] = 'Secret';

echo strlen(http_build_query($data));

curl_setopt($ch, CURLOPT_URL, '*/upload.php');

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));


and the header of this request is


[CONTENT_TYPE] => application/x-www-form-urlencoded

so it is better to use application/x-www-form-urlencoded than multipart/form-data for it is easy to calculate content-length

Request not using CURLOPT_POSTFIELDS have content-length set , I don't know if this is a normal behaviour but if I send a large body (a file > The real problem is here  When talking to a HTTP 1.1 server, you can tell curl to send the request body without a Content-Length: header upfront that specifies exactly how big the POST is. By insisting on curl using chunked Transfer-Encoding, curl will send the POST "chunked" piece by piece in a special style that also sends the size for each such chunk as it goes along.

Curl POST - 411 Length Required, You are correct -- lighttpd doesn't support POST requests with an empty message body without a 'Content-Length' header set to zero, and  When setting CURLOPT_POST to 1, libcurl will automatically set CURLOPT_NOBODY and CURLOPT_HTTPGET to 0. If you issue a POST request and then want to make a HEAD or GET using the same re-used handle, you must explicitly set the new request type using CURLOPT_NOBODY or CURLOPT_HTTPGET or similar.

Can't post data to rest server using cURL with content length larger , Hi, I am using services module, rest and i love it. But I have run into issue with posting image larger than 800kB. Content length of this request is  Content-Length: 363. 3 thoughts on “ View header and body with curl ” José Borges says: 24 May 2016 at 2:01 pm Post navigation

How to send POST with body, headers, and HTTP params using , Headers are in -H and body which is a xml file is in ${aclfile} following -T. You can see PUT /aaa/?acl HTTP/1.1 > User-Agent: curl/7.29.0 > Host: 192.168.​57.101 Content-Length: 323 > Expect: 100-continue > < HTTP/1.1 100 CONTINUE  -w, --write-out <format> Make curl display information on stdout after a completed transfer. See man page for more details on available variables. Convenient way to force curl to append a newline to output: -w " " (can add to ~/.curlrc ). -X, --request The request method to use. When sending data via a POST or PUT request, two common formats

  • This does not address my issue as post_body is an array so $strLen does not work
  • A HTTP POST's body is just ascii. It has no idea what an array is. You convert the array to a query string and get the string's length.
  • $postData is an Array and Length on Array does not work
  • @MisterniceGuy answer has been updated, try using this example and see if it works for you.