【问题标题】:how to send correct curl command to webserver如何向网络服务器发送正确的 curl 命令
【发布时间】:2016-05-17 08:15:45
【问题描述】:

所以我得到了正在发送到特定服务器的数据。现在,我想在本地机器上使用 curl 来做同样的事情,以处理来自服务器的特定响应,并了解有关 curl 的更多信息。

这是我的部分数据

POST /auth HTTP/1.1
platform: android
X-Auth-Token: <censored>
Content-Type: application/json; charset=utf-8
Host: api.blabla.com
Accept-Encoding: gzip

以及正在发送的数据:

{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}

现在,当我在我的 dos shell 中尝试 cURL 时,我会尝试

curl --insecure -X POST https://api.blabla.com/auth --data '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}'

我从 cURL 得到的响应是这样的:

{"code":401,"error":"blablaTokenRequired"}

即使我指定了令牌。所以有两种可能的情况,因为token是正确的:

  1. 这与 SSL 的事情有关吗? (我使用 --insecure 是因为否则会出现 SSL 错误)
  2. 我的命令有些地方不正确,但我不知道是什么。

有人可以帮帮我吗?我正在尽我所能,但没有成功

【问题讨论】:

  • 我看不出这对我尝试发送的 POST 数据有何帮助?
  • 让 curl 黑客高兴,并且当您已经使用 --data 时,不要使用 -X POST,这意味着 POST 本身...

标签: api curl


【解决方案1】:

我不确定我是否理解您的应用特定权利,但您可能需要考虑一件事:

男人卷曲说:

-d, --data <data>
             (HTTP)  Sends the specified data in a POST request to the HTTP    server, in the same way that a browser does when
          a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data  to  the
          server using the content-type application/x-www-form-urlencoded.  Compare to -F, --form.

          -d,  --data  is the same as --data-ascii. --data-raw is almost the same but does not have a special interpreta‐
          tion of the @ character. To post data purely binary, you should instead use the --data-binary option.  To  URL-
          encode the value of a form field you may use --data-urlencode.

我在您的示例中看不到将数据作为 HTML 表单输入发送的必要性,可能您的应用程序预计只是一个“原始”POST 正文,然后您必须尝试以下操作:

curl --insecure -X POST https://api.blabla.com/auth --data--binary '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}'

PS 并且肯定这是错误不是关于使用 --insecure 只是要求 curl 忽略 ssl 验证

【讨论】:

  • --insecure 只会让 curl 忽略任何 ssl 证书验证错误,并信任一切:p
  • 是的,你是对的。我不准确。但我只想留下,似乎使用 --insecure 不会导致 404 错误,至少它看起来像这样。
【解决方案2】:

您忘记了标头并启用了压缩编码 (gzip),但是,我相信您不能强制 curl 仅使用 curl 命令行支持 gzip 编码,您必须使用 libcurl,这将使请求说“Accept-Encoding: gzip,deflate” 在大多数系统上,使用 --compressed .. 如果您不接受,请使用 libcurl 重写它(如果您愿意,可以通过 CURLOPT_ENCODING 强制它只说“gzip”)

curl -X POST https://api.blabla.com/auth --data '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}' --header 'platform: android' --header 'X-Auth-Token: <censored>' --header 'Content-Type: application/json; charset=utf-8' --header 'Host: api.blabla.com' --compressed

另一个问题:在某些系统上,会有一个默认的用户代理标头(如 debian 6),而在某些系统上,curl 没有默认的用户代理(如 debian 8).. 你可能想使用 --user-代理''太

【讨论】:

  • curl(命令行工具)在所有平台上都有一个默认的用户代理,只是可能略有不同
猜你喜欢
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2012-07-27
  • 2017-01-01
  • 1970-01-01
  • 2012-06-29
  • 2017-11-02
  • 2016-04-27
相关资源
最近更新 更多