问题
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;
- 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
- 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