2
<?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 ?

4
  • 1
    Think you could add what you are getting vs. what you expect? Commented Jun 1, 2018 at 16:23
  • @Jason Update the descrbie . Give some advice if you are conviniece ,thanks a lot . Commented Jun 1, 2018 at 16:41
  • Is that top code block the contents of text.txt? Commented Jun 1, 2018 at 17:24
  • @json ,Yes , the variable $uploadPath is the path of text.txt Commented Jun 2, 2018 at 3:41

1 Answer 1

6

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.