How to get request body without content-length? - php

Is there any way to retrieve a request body when a content-length header is not included in the request?
for example, I have this in /var/www/test.php:
print_r(apache_request_headers());
$data = file_get_contents('php://input');
var_dump($data);
And run this command:
curl -X POST -d test --header 'Content-Length: ' localhost/test.php
I see that there is no content-length header, and $data is empty. If I specify, for instance, a content-length of '3', I get an output for $data of 'tes'.
Is there any way to make php retrieve the request body, irrespective of the content-length header?

Is there any way to retrieve a request body when a content-length
header is not included in the request?
Yes. But you are including it in the request.
curl -d test localhost/test.php should return:
Array
(
[User-Agent] => curl/7.xx.x
[Host] => localhost
[Accept] => */*
[Content-Length] => 4
[Content-Type] => application/x-www-form-urlencoded
)
string(4) "test"
You can just omit sending the header.

Related

Curl not working with post param and application/json type

I am trying this command
curl -i -X POST -H 'Content-Type: application/json' \
-d '{"user":[{"name":"name","phone_number":9934432222},{"name":"name","phone_number":9934432222},{"name":"name","phone_number":9934432222}]}' \
http://local.com/apis/get_user_list
And printing data with print_r($_POST); but output is like
<pre>Array
(
)
Why its not printing post parameters?
I also tried with rest console, but in output of response header is like:
Status Code: 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 35
Content-Type: text/html
And output is same empty.
When you POST data like JSON which isn't application/x-www-form-urlencoded or multipart/form-data, it may not be available in POST.
You can access the JSON using
$string = file_get_contents('php://input');
var_dump(json_decode($string));

file_get_contents Default headers

I was wondering what is the default Content-Type header option when requesting a json string from an API.
I've noticed even if the requested Content-Type is application/json the response header Content-Type will be text/html
Does file_get_contents set the headers automatically based on what it is requesting or does it have a default one that is used always?
Does file_get_contents set the headers automatically based on what it is requesting or does it have a default one that is used always?
I think no defaults. For the context parameter you read below and see the 4-th example in docs:
A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL.
Tested with Wireshark.
PHP 5.5.8 sends:
POST /path HTTP/1.0
Host: example.com
Content-Length: [appropriate number]
Content-Type: application/x-www-form-urlencoded
For the following context:
$options = array(
'http' => array(
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create($options);

Onedrive uploading fails with the Internal Server Error

I'm trying to create file on the OneDrive using REST API with PHP, but in the response I retrieve HTTP status code 500.
Code:
`
$url = $this->buildUrl(
'{folder_id}/files/{filename}?access_token={token}',
array(
'folder_id' => $folderId,
'filename' => $filename,
'token' => $this->getAccessToken(),
)
);
$response = wp_remote_request($url, array(
'body' => $content,
'method' => 'PUT',
));
`
Error message from the response body:
An error occurred while performing the action. Try again later.
What i'm doing wrong?
I just went through the same problem. It worked for me when I removed 'Content-type' line from request header.
If you are using PHP Curl to send request in wp_remote_request, you can remove 'Content-type' line from request header by calling something similar to this, before calling curl_exec:
curl_setopt($ci, CURLOPT_HTTPHEADER, array("Content-Type:"));
By adding the code above, the actual request header looks like this (note there is no 'Content-Type'):
PUT /v5.0/{folderId}/files/{filename}?access_token={accesstoken}
User-Agent: SOMEAGENT
Host: apis.live.net
Accept: */*
Expect: 100-continue
Content-Length: 29
FYI: I got a hint from here:
http://msdn.microsoft.com/en-us/library/dn631834.aspx
"For a PUT request, leave the Content-Type blank and put the contents of the file in the request body."
Hope it helps.

how to stop curl php from sending accept header

By default curl sends Accept: */* header for all requests. How do I stop sending of the default header?
Accept: */*
Pass in "Accept:" (ie a header with no contents to the right of the colon) with CURLOPT_HTTPHEADER. Like:
$headers = array( "Accept:" );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
The equivalent using the curl command line tool is:
curl -H 'Accept:' http://example.com/
This will make curl remove the header, not just send a header with a blank value. You can also send a header with a blank value if you want, but then you need to use a semicolon instead of colon!
Make sure your array item is set up with the colon immediately following the header name, like this:
array( "Accept: yourAcceptValue" )
Not with spaces, like this:
array( "Accept : yourAcceptValue" )
If there is a space between the header name and the colon, curl will add the default */* into your headers.
(Works in PHP 5.5.9-1ubuntu4.7)

There is an empty file after updating

I'm trying to update an existing document with help of Google Docs API. I'm using PHP+Curl. The gdata.class.php is base class. Updating finishes partly. A old content is cleaned. But a new content is not created. According to documentation, I send PUT-request (initial request) to address
http://docs.google.com/feeds/upload/create-session/default/private/full/document%123456
There are Etag of the document and the empty request body. I recive the status code 200 Ok and the unique upload URI like this:
http://docs.google.com/feeds/upload/create-session/default/private/full/document%123456?upload_id=EnB2Uob7DWcFVJTX3oF8sdVv9koZTHacngmM_
What should I do???
I'm sending the file content and headers to the unique upload URI:
[0] => Content-Length: 6
[1] => Content-Range: bytes 0-5/6
[2] => Content-Type: text / plain
The responce is recived:
HTTP/1.1 308 Resume Incomplete
Headers not contains the "Range" header. It's strange.
Result: the target file is not changed.
I'm sending the file content and headers to the unique upload URI:
[0] => Content-Length: 6
[1] => Content-Type: text/plain
The responce is recived:
HTTP/1.1 200 OK
There is atom+xml in the body.
Result: the target file became an empty.
P.s. The curl_getinfo function returns "5". It's not depends on size of the file.
Well if it asks you to use PUT method, then tell CURL to use it..
curl_setopt($ch, CURLOPT_PUT, 1);

Categories