In PUT request how to retrieve data in codeigniter 3 - php

I'm sending data as http PUT request. Using Postman I have chosen form-data as option to send data as I want to send(upload) file as well,along with other necessary data.
I have chosen a csv file and a field with name(key) as "asd".
In output window data is displayed as below :
So my question is : how to retrieve or receive the data in codeigniter 3 with key name asd and input type file's key name fil?
I tried:
$this->input->raw_input_stream('asd');
$this->input->raw_input_stream('fil');
But it didn't work.
------WebKitFormBoundaryNXgITT259B576h8U
Content-Disposition: form-data; name="asd"
zxxxxxxxxxxxx
------WebKitFormBoundaryNXgITT259B576h8U
Content-Disposition: form-data; name="fil"; filename="utility_type.csv"
Content-Type: text/csv
Uid,Description,Modified
3,"Water and Sewer","2009-10-08 20:49:46"
2,"Combined Services","2014-07-29 14:50:32"
4,Electric,"2011-08-30 20:12:52"
5,"Natural Gas","0000-00-00 00:00:00"
6,Cable/Internet,"2015-06-10 17:47:51"
7,"Solid Waste/Trash Collection","2009-07-09 21:19:38"
30,Sewer,"2012-05-28 11:57:32"
80,Water,"2016-10-26 17:09:57"
------WebKitFormBoundaryNXgITT259B576h8U--

If it is a HTTP request you could use:
file_get_contents('php://input')
It will return the HTTP body.

Related

how to read data sent from post Content-Type: multipart/form-data; boundary=

can you guy's help me with this plz
i have data sent from post Content-Type: multipart/form-data; boundary=
-poster.php is sending post
-reciver.php is the file im posting to
--------------------------42281d81075fd0d5
Content-Disposition: form-data; name="one"
this is one
--------------------------42281d81075fd0d5
Content-Disposition: form-data; name="two"
this is two
--------------------------42281d81075fd0d5--
so what i want to know is how i can get those values
i dont think $_POST[''];
i can't figur it out
thank you for your help
You can access the values by $_POST['one'] and $_POST['two'] in case of normal form data.
If you want to read a file that you uploaded you can access this by using $_FILES['name_of_file_input_field'].
when you using some php framework and jQuery then you receiving your data like that. You might jQuery ajax setting with contentType set to false. Then you can get the data like you want.

php curl send multiple data under one key

How to send data via curl which is under select tag? I can't find answer, please help, browser's procedure as follows:
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryEbHiNmjjU3pgp1Tb
------WebKitFormBoundaryEbHiNmjjU3pgp1Tb
Content-Disposition: form-data; name="allVolumeCountries"
6
------WebKitFormBoundaryEbHiNmjjU3pgp1Tb
Content-Disposition: form-data; name="allVolumeCountries"
13
P.S: I don't have access to source, so don't advise me to change to something like allVolumeCountries[]

POST request with files and fields in guzzle

Ok I'll give a little background to start. I have system A written in CakePHP that handles ads and products and such. Recently I have been working on another system, written in Laravel, that acts as a self-serve tool for realtors to post and manage their real estate listings that reside in system A. I am now at the point of uploading images from the self-serve site to system A. I wrote a simple controller action in Cake to handle a POST request and save the image file on the server.
http://example.com/image/add
I am able to send POST requests, upload images and get a proper response using REST applications such as postman. All looks good on system A (Cake) side of things.
Now in the self-serve system, in Laravel, I am using Guzzle to send HTTP requests. I have filled out the guzzle post request with the exact same fields and files, but I don't recieve the same output. The request is received by system A, but the image is not added and a random HTML page is returned. If postman and a few other applications get the exact same response and functionality, but my request sent in Guzzle is not, I am thinking there is an issue with my guzzle request. Here is my guzzle code:
$client = new Client();
// Create the request.
$request = $client->createRequest("POST", "http://example.com/image/add");
// Set the POST information.
$postBody = $request->getBody();
$postBody->setField('user_id', $userId);
$postBody->setField('api_key', $token);
$postBody->setField('product_id', $product_id);
$postBody->addFile(new PostFile('image[data]', fopen('tmp/images/'.$input->image, 'r')));
// Send the request and get the response.
$response = $client->send($request);
Here is the working POST request from Postman:
POST /image/add HTTP/1.1
Host: example.com
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="product_id"
1000
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="user_id"
8345
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="api_key"
secretKey
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="data[image]"; filename="steve-jobs.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Here is the guzzle request to string, I apologize for readability issues.
POST /image/add HTTP/1.1 Host: example.com User-Agent: Guzzle/4.0.2 curl/7.22.0 PHP/5.5.9-1+sury.org~precise+1 Content-Length: 124647 Content-Type: multipart/form-data; boundary=53a1f21e04080 --53a1f21e04080 Content-Disposition: form-data; name="user_id" 8345 --53a1f21e04080 Content-Disposition: form-data; name="api_key" secretKey --53a1f21e04080 Content-Disposition: form-data; name="product_id" 1000 --53a1f21e04080 Content-Disposition: form-data; filename="steve-jobs.jpg"; name="image[data]" Content-Type: image/jpeg
Then a bunch a characters for the image data.
I am asking if anyone can see an issue with my POST request in guzzle or if anyone has run into this kind of weird issue with guzzle before.
Edit:
I am using CakePHP 2.4.1
As far as I can tell it's just a small mistake in the file field name, it should be data[image], not image[data].
new PostFile('data[image]', /* ... */);
In your app you are probably relying on the file data being available via CakeRequest::$data, however it will only land there in case it's wrapped in the data key, otherwise it is added to CakeRequest::$params['form'].
Not really a duplicate, however for (a more CakePHP form helper related) reference see also:
CakePHP: posted file data not included in request->data

Php POST - get boundary value

So I want to upload a file using curl, but the problem is: the post request contains a boundary variable that is checked upon by the website. I know this because I get errors when modifying this variable in Tamper Data.
Http headers (important part):
Content-Type: multipart/form-data; boundary=----pluploadboundaryp18mu3sf5fus08pd11c3kvq163514
Post request:
------pluploadboundaryp18mu3sf5fus08pd11c3kvq163514\r\n
Content-Disposition: form-data; name="name"\r\n
\r\n
1.jpg\r\n
------pluploadboundaryp18mu3sf5fus08pd11c3kvq163514\r\n
Content-Disposition: form-data; name="imageData"; filename="1.jpg"\r\n
Content-Type: image/jpeg\r\n
\r\n
{Filecontent}
So my question is: How can I bypass or duplicate this boundary?
Edit: if you want to test yourself the form upload is located at:https://www.marktplaats.nl/syi/239/1399/plaatsAdvertentie.html
So i got the answer on my question. Also thanks to Ruslan Bes for helping.
The problem was i sended my post request as http_build_query($post) instead of $post.
Small mistakes can cause a lot of problems :D

How to properly format body for post request?

I'm using a firefox pluing called restclient to simulate a post request. It doesnt' seem to be picking up any post data, but not sure if I'm formatting it properly.
using header: Content-Type: application/json
and body: {"id":1234}
but not go, it's not picking up the id parameter in my php, is there some special formatting I need to set?
okay, got it working, here is what is needed
two content types:
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
and then set your params like so in body:
param1=value1&param2=value2
Thanks for the help everyone.
PHP will not parse a JSON body automatically into the $_POST superglobal. That only happens with application/x-www-form-urlencoded and multipart/form-data POST bodies. That said, you can parse the body yourself — you can access the raw POST body via the php://input pseudo-stream.

Categories