curl:-d和--data-binary选项之间有什么区别? [英] curl: What's the difference between -d and --data-binary options?

查看:6118
本文介绍了curl:-d和--data-binary选项之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向REST Api发送发布请求.我注意到当我在curl中使用-d选项传递参数时,一切工作正常.示例:

I'm trying to send a post request to a REST Api. I noticed that everything works fine when I pass parameters with -d option in curl. Example:

curl "https://mywebsite.com" -d "param1=x" -d "param2=y" -u "3SUHZb0sanKWrQ"

但是,如果将参数作为json对象并使用--data-binary发送,则我会收到来自Api的错误消息(就像没有接收到任何参数一样).示例:

However, if a send parameters as a json object and using --data-binary, I receive an error from the Api (as if no parameters were received). Example:

curl "https://mywebsite.com" --data-binary $'{ "param1": "x", -d "param2":"y" }' -u "3SUHZb0sanKWrQ"

我认为这两种方法具有相同的行为,但是我认为我错了.这两种方法有什么区别?

I thought the two approaches had the same behaviour, but I think I'm wrong. What's the difference between these two approaches?

P.S .:第二个请求是我在Google chrome上选择复制为卷曲"选项时收到的curl请求,因为实际请求是angularJS中的$ http.post,其数据有效载荷作为json对象.我该如何在angularJS中使它正常工作?

P.S.: the second request is the curl request that I get when select "copy as curl" option on google chrome, because the actual request is a $http.post in angularJS with its data payload as a json object. What can I do in angularJS to get it working?

var data = { 
  "param1": "x", 
  "param2": "y" 
};

$http({
    url: "https://mywebsite.com",
    method: 'POST',
    data: data
}).then(function successCallback(response){
    console.log(response);
}, function errorCallback(response){
    console.log(response);
});

谢谢!

推荐答案

这是我通过curl --help所获得的:

 -d, --data DATA     HTTP POST data (H)
     --data-raw DATA  HTTP POST data, '@' allowed (H)
     --data-ascii DATA  HTTP POST ASCII data (H)
     --data-binary DATA  HTTP POST binary data (H)
     --data-urlencode DATA  HTTP POST data url encoded (H)
     --delegation STRING  GSS-API delegation permission
     --digest        Use HTTP Digest Authentication (H)
     --disable-eprt  Inhibit using EPRT or LPRT (F)
     --disable-epsv  Inhibit using EPSV (F)
     --dns-servers   DNS server addrs to use: 1.1.1.1;2.2.2.2
     --dns-interface  Interface to use for DNS requests
     --dns-ipv4-addr  IPv4 address to use for DNS requests, dot notation
     --dns-ipv6-addr  IPv6 address to use for DNS requests, dot notation§

因此,区别仅在于-d数据发送时不是二进制内容.

So, the difference is just that with -d data sent is not binary content.

这篇关于curl:-d和--data-binary选项之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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