Is it possible to use `--data-urlencode` and `--data-binary` options for the same curl command?

谁说胖子不能爱 提交于 2020-07-24 03:17:53

问题


I am using curl and I would like to execute a HTTP PUT request by sending a --data-urlencode string and a --data-binary JSON file content. Is it possible to make that in the same curl command?

I tried the following

curl www.website.org --request PUT -H Content-Type: application/json --data-urlencode "key=sample_string" --data-binary @sample_file.json

but it seems do not work as expected: key=sample_string and sample_file.json content are not send at all.


回答1:


A couple of things here;

  1. Your curl request is missing double quotes for the headers. It should rather be:
curl www.website.org --request PUT -H "Content-Type: application/json" \
 --data-urlencode "key=sample_string" --data-binary @sample_file.json
  1. Your content-type is application/json which I hope is not "binary", so you should rather be using an appropriate type.

In any case, you should be able to find the submitted values using a simple php script as follows:

$putfp = fopen('php://input', 'r');
$putdata = '';
while($data = fread($putfp, 1024))
    $putdata .= $data;
fclose($putfp);

var_dump($putdata);

echo "---CONTENT_TYPE---\n";
var_dump($_SERVER['CONTENT_TYPE']);


来源:https://stackoverflow.com/questions/24507051/is-it-possible-to-use-data-urlencode-and-data-binary-options-for-the-sam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!