是否可以对同一个curl命令使用`--data-urlencode`和`--data-binary`选项? [英] Is it possible to use `--data-urlencode` and `--data-binary` options for the same curl command?

查看:129
本文介绍了是否可以对同一个curl命令使用`--data-urlencode`和`--data-binary`选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 curl ,我想通过发送执行HTTP PUT请求--data-urlencode字符串和--data-binary JSON文件内容.是否可以在同一curl命令中做到这一点?

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?

我尝试了以下

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

,但似乎无法按预期工作:key=sample_stringsample_file.json内容根本没有发送.

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

推荐答案

这里有几件事;

  1. 您的curl请求缺少标头的双引号.它应该是:

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

  1. 您的内容类型是application/json,我希望它不是二进制",因此您应该使用适当的类型.

无论如何,您应该可以使用如下所示的简单php脚本查找提交的值:

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']);

这篇关于是否可以对同一个curl命令使用`--data-urlencode`和`--data-binary`选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆