【问题标题】:Seeking correct syntax for curl POST command that puts out a JSON record为输出 JSON 记录的 curl POST 命令寻求正确的语法
【发布时间】:2014-08-10 16:03:00
【问题描述】:

这是我用于 curl POST 命令的语法:

curl -d 'username:"admin",password:"secret"' -H "Content-Type: application/json" http://xxx.xxx.me:9080/v2.0/tokens

我运行它,它在客户端回显:

{"用户名:\"admin\",密码:\"secret\"":""}

我期待:

{"用户名:\"aadmin\",密码:\"secret\"}

我做错了什么?

【问题讨论】:

    标签: json post curl wget


    【解决方案1】:

    试试这个:

    curl -i -H "Content-Type: application/json" -X POST -d '{"username":"admin", "password":"secret"}' http://xxx.xxx.me:9080/v2.0/tokens
    

    【讨论】:

    • 不幸的是,不好:{"{\"username\":\"admin\", \"password\":\"secret\"}":""}
    【解决方案2】:

    试试这个..

     curl -X POST -H "Content-Type: application/json" -d '{"username":"admin", "password":"secret"}' http://xxx.xxx.me:9080/v2.0/tokens
    

    【讨论】:

    • 运气不好:{"{\"username\":\"admin\", \"password\":\"secret\"}":""}
    【解决方案3】:

    我通过反复试验解决了这个问题。

    用于输出 JSON 记录的 curl POST 语法:

    curl -d 'username=admin&password=secret' http://xxx.xxx.me:9080/v2.0/tokens

    输出 JSON 记录的 wget POST 语法:

    wget --post-data='username=admin&password=secret' http://xxx.xxx.me:9080/v2.0/token

    在 Mac (OSX 10.7) 上, curl --version 为 7.21.4,在 Linux Centos 6.2 上, curl --version 为 7.19.7

    在 Mac (OSX 10.7) 上,wget --version 为 1.13.4,在 Linux Centos 6.2 上,wget --version 为 1.12

    后端服务器是 node.js 版本是 v0.10.21 但我不认为后端很重要 - 后端可能是 PHP Apache 服务器是什么。

    我知道我在 2014 年 8 月 12 日这一天与官方的答案存在矛盾,但我不知道该如何解释这种矛盾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 2018-07-08
      • 2019-01-07
      • 2014-08-06
      • 1970-01-01
      • 2020-12-03
      • 2013-04-28
      相关资源
      最近更新 更多