2011-11-25 21:55:40 +0000 2011-11-25 21:55:40 +0000
40
40

如何使用cURL发送带有body、headers和HTTP params的POST?

我找到了 很多关于如何在 cURL中使用简单的POST命令的例子,但是我没有找到如何发送完整的 HTTP POST命令的例子,这些命令中包含了

  • Headers(基本认证)
  • HTTP Params(s=1&r=33
  • Body Data,一些XML字符串

我找到的都是:

echo "this is body" | curl -d "ss=ss&qq=11" http://localhost/

那不行,它把 HTTP参数作为body发送。

答案 (2)

58
58
58
2011-11-25 22:24:02 +0000

HTTP “参数 "是URL的一部分。

"http://localhost/?name=value&othername=othervalue"

基本认证有一个单独的选项,没有必要创建一个自定义头。

-u "user:password"

POST "body "可以通过--data(application/x-www-form-urlencoded)或--form(multipart/form-data)发送:

-F "foo=bar" # 'foo' value is 'bar'
-F "foo=<foovalue.txt" # the specified file is sent as plain text input
-F "foo=@foovalue.txt" # the specified file is sent as an attachment

-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=@foovalue.txt"
-d "@entirebody.txt" # the specified file is used as the POST body

--data-binary "@binarybody.jpg"

所以,总结一下:

curl -d "this is body" -u "user:pass" "http://localhost/?ss=ss&qq=11"
``` POST "body
16
16
16
2016-08-19 02:59:31 +0000

名气不够,无法评论,所以留下这个作为答案,希望对大家有所帮助。

curl -L -v --post301 --post302 -i -X PUT -T "${aclfile}" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  ${host}:${port}${resource}

这是我在S3 bucket acl put操作中使用的方法。头文件在-H中,正文是一个xml文件,在-T后面的${aclfile}中。你可以从输出中看到这一点。

/aaa/?acl
* About to connect() to 192.168.57.101 port 80 (#0)
* Trying 192.168.57.101...
* Connected to 192.168.57.101 (192.168.57.101) port 80 (#0)
> PUT /aaa/?acl HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.57.101
> Accept: */*
> Date: Thu, 18 Aug 2016 08:01:44 GMT
> Content-Type: application/x-www-form-urlencoded; charset=utf-8
> Authorization: AWS WFBZ1S6SO0DZHW2LRM6U:r84lr/lPO0JCpfk5M3GRJfHdUgQ=
> Content-Length: 323
> Expect: 100-continue
>
< HTTP/1.1 100 CONTINUE
HTTP/1.1 100 CONTINUE

* We are completely uploaded and fine
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< x-amz-request-id: tx00000000000000000001f-0057b56b69-31d42-default
x-amz-request-id: tx00000000000000000001f-0057b56b69-31d42-default
< Content-Type: application/xml
Content-Type: application/xml
< Content-Length: 0
Content-Length: 0
< Date: Thu, 18 Aug 2016 08:01:45 GMT
Date: Thu, 18 Aug 2016 08:01:45 GMT

<
* Connection #0 to host 192.168.57.101 left intact

如果url参数包含特殊符号,比如 “+",则对其中的每个参数(包含特殊符号)使用–data-urlencode。

curl -G -H "Accept:..." -H "..." --data-urlencode "beginTime=${time}+${zone}" --data-urlencode "endTime=${time}+${zone}" "${url}"