Reputation: 21
<?php
$post_data = array(
'filename'=>new \CurlFile($uploadPath),
'jsondata'=> json_encode($params,JSON_UNESCAPED_UNICODE)
$toPdfApi='https://example.com/convert2PDF'
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$toPdfApi);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_BINARYTRANSFER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'multipart/form-data',
'application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS,$post_data);
$result = curl_exec($curl);
Then I convert it to bash shell command
curl -i -X POST -H "Content-Type: multipart/form-data" -H "Content-Type: application/x-www-form-urlencoded" --data-binary 'filename=@/root/test.txt' --data-binary 'jsondata={"fileName":"test","id":"xxx","backlink":"https://example.com/hello"}' https://example.com/convert2PDF
But the reponse result still not right
HTTP/1.1 200 OK
Date: Fri, 01 Jun 2018 14:41:37 GMT
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked
Server: Jetty(9.4.5.v20170502)
error
Can someone help ? is it my covert bash shell right?
if I use the following command [replace -data-binary to -F ], I get the right status ,but from the php code I know I should add the CURLOPT_BINARYTRANSFER in bash curl ,otherwise the https://example.com/convert2PDF can not get the right post data.
curl -i -X POST -H "Content-Type: multipart/form-data" -H "Content-Type: application/x-www-form-urlencoded" -F 'filename=@/root/test.txt' -F 'jsondata={"fileName":"test","id":"xxx","backlink":"https://example.com/hello"}' https://example.com/convert2PDF
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Fri, 01 Jun 2018 16:29:35 GMT
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked
Server: Jetty(9.4.5.v20170502)
success
I want to get the reponse's status is 200 ,and the result is success in --data-binary mode ,What can I do to udpate my bash command ?
Upvotes: 2
Views: 12032
Reputation: 8616
The whole point of --data-binary
is that it's one big lump of bytes. You can only send one as each HTTP request only has one body and can only have one top-level content type.
What it seems you're trying to do is construct a multi-part request body that has two parts, one containing file data and one containing key/value pairs.
To send a file with some metadata over command line, you can do this:
curl -F'id=xxx' -F 'name=foo' -F'[email protected];type=text/plain' http://example.com
That will send a multi-part request where a PHP backend could access the metadata from $_POST and the file data from $_FILES.
The exact approach required would depend on what the back end expects to receive but you've not posted that.
For example, the following would work with --data-binary (as per your question) but the parameters and the file data would both be accessed in a different way on the back end.
curl --data-binary '@test.txt' 'http://example.com?id=xxx&name=foo'
Upvotes: 6