【问题标题】:Understanding curl POST request command that contains multiple Content-Type headers了解包含多个 Content-Type 标头的 curl POST 请求命令
【发布时间】:2016-04-11 04:47:11
【问题描述】:

以下 curl 命令:

curl -v -F 'json={"method":"update_video","params":{"video":{"id":"582984001","itemState":"INACTIVE"},"token":"jCoXH5OAMYQtXm1sg62KAF3ysG90YLagEECDAdlhg.."}}' https://api.somewebservice.com/services/post

产生这个输出:

{"method":"update_video","params":{"video":{"id":"55269001","itemState":"INACTIVE"},"token":"jCoXH1sg62KAF3ysG90YLagEECTP16uOUSg_fDAdlhg.."}}' https://api.somewebservice.com/services/post
*   Trying 64.74.101.65...
* Connected to api.somewebservice.com (64.74.101.65) port 443 (#0)
* TLSv1.0, TLS handshake, Client hello (1):
* TLSv1.0, TLS handshake, Server hello (2):
* TLSv1.0, TLS handshake, CERT (11):
* TLSv1.0, TLS handshake, Server key exchange (12):
* TLSv1.0, TLS handshake, Server finished (14):
* TLSv1.0, TLS handshake, Client key exchange (16):
* TLSv1.0, TLS change cipher, Client hello (1):
* TLSv1.0, TLS handshake, Finished (20):
* TLSv1.0, TLS change cipher, Client hello (1):
* TLSv1.0, TLS handshake, Finished (20):
* SSL connection using TLSv1.0 / DHE-RSA-AES256-SHA
* Server certificate:
*    subject: OU=Domain Control Validated; OU=Issued through Somewebservice Inc. E-PKI Manager; OU=COMODO SSL; CN=api.brightcove.com
*    start date: 2015-09-02 00:00:00 GMT
*    expire date: 2016-10-09 23:59:59 GMT
*    subjectAltName: api.somewebservice.com matched
*    issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
*    SSL certificate verify ok.
> POST /services/post HTTP/1.1
> User-Agent: curl/7.41.0
> Host: api.somewebservice.com
> Accept: */*
> Content-Length: 294
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------5106835c8f9f70f9
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
< Content-Length: 943
< Date: Sun, 10 Apr 2016 22:29:23 GMT
< Server: somewebservice
<
* Connection #0 to host api.somewebservice.com left intact
{"result": {"id":55225001,"name":"Taxpayers pay to cover tattoos","adKeys":null,"shortDescription":"Opening statements are set to begin in the trial.","longDescription":null,"creationDate":"1260220396","publishedDate":"12603101609","lastModifiedDate":"1460352526","linkURL":null,"linkText":null,"tags":["Crime","national","wtsp","neo-nazi","court","taxpayers","News","David","john"],"videoStillURL":"http:\/\/bcdownload.net\/wtsp\/35134001\/3508134001_55110080001_59001.jpg?pubId=35134001","thumbnailURL":"http:\/\/bcdownload.edgesuite.net\/wtsp\/87134001\/350134001_55110081001_th-55100159001.jpg?pubId=35084001","referenceId":"7cf007503e2ee37a","length":112106,"economics":"AD_SUPPORTED","playsTotal":248,"playsTrailingWeek":0}, "error": null, "id": null}

上面的输出中有两个“Content-Type”对象:

> Content-Type: multipart/form-data;

和

< Content-Type: application/json;charset=UTF-8

根据docs,使用-F 允许curl 以多部分形式发送数据,但是json= 是我在文档中找不到的。我假设它正在转换字典/字符串:

{"method":"update_video","params":{"video":{"id":"582984001","itemState":"INACTIVE"},"token":"jCoXH5OAMYQtXm1sg62KAF3ysG90YLagEECDAdlhg.."}}

到 JSON?或者更准确地说,它是将'Content-Type': 'application/json' 标头添加到POST 请求中?那么这本质上是一个带有两个不同标头的 POST 请求吗?

【问题讨论】:

    标签: json post curl http-headers


    【解决方案1】:

    第一个Content-Type 标头是客户端request 标头的一部分,第二个标头是服务器response 标头的一部分。请求和响应由 2 个 CRLF 分隔。每个请求和响应都有自己的 Content-Types。

    【讨论】:

    • 好的,感谢您解决这个问题。因此,使用此 curl 命令执行的实际 POST 请求的标头为 'Content-Type': 'multipart/form-data' ?
    • json= 实际上看起来像键/值对的 key= 部分,与您可能习惯在 HTTP GET 请求中的查询字符串中看到的相同。实际上,如果您以这种方式提交值部分(即实际的 json 数据)应该是 url 编码的,因为如果 json 恰好包含 & 符号,您的 json 将被截断。
    • 以这种方式设计 REST API 对我来说似乎有点愚蠢,但如果服务器给你一个 200 OK 响应,那么我猜你的请求是正确的。更合理的 REST API 设计会让您只发布 json 数据(前面没有 json=)并在请求标头中使用 Content-Type: application/json。
    • 哦,您仍然需要使用 HTTP POST 而不是 GET,只是不需要它是多部分的。将-F 更改为--data。
    • 太棒了,感谢今晚的所有帮助,非常感谢!
    猜你喜欢
    • 2021-02-12
    • 2011-10-19
    • 2014-12-01
    • 2014-12-02
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    相关资源
    最近更新 更多