【问题标题】:Using Curl with Json (Node.js)使用带有 Json 的 Curl (Node.js)
【发布时间】:2017-07-07 22:19:41
【问题描述】:

当我在curl 中省略JSON-RPC 请求参数的“[ 和]”部分时,我无法让curlJson (Node.js) 工作。我正在使用命名参数(即一个对象,而不是一个数组)。

这行得通:

curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": ["{\"username\":\"morten10\",\"password\":\"mypass\"}"], "id":1 }' \
http://localhost:3000

但是,当我从参数中省略 [" 和 ]" 时,它不起作用:

curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": {\"username\":\"morten10\",\"password\":\"mypass\"}, "id":1 }' \
http://localhost:3000

Jayson 给出了这个错误信息:

TypeError: First argument must be a string or Buffer

但是,根据JSON-RPC 2.0 规范及其示例,当我使用命名参数时,我应该能够从参数中排除“[和]”部分,如下例所示:

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}

我没有得到什么?

谢谢!

【问题讨论】:

    标签: node.js curl json-rpc


    【解决方案1】:

    我认为您的对象需要是错误提示的字符串。

    试试:

    curl -v -i POST -H "Content-Type: application/json" \
    -d '{"jsonrpc": "2.0", "method":"registerPerson", "params": "{\"username\":\"morten10\",\"password\":\"mypass\"}", "id":1 }' \
    http://localhost:3000
    

    【讨论】:

    • 谢谢,但我已经试过了。但更改为:curl -v -i POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method":"registerPerson", "params": "{\"username\":\"morten10\",\"password\":\"mypass\"}", "id":1 }' http://localhost:3000 让杰森给出这条消息(在我的代码到达之前):{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}} 去图。
    • 有趣的是,Jayson 在该错误响应中将“id”设置为 null。根据 JSON-RPC 2.0 规范,只有在请求中检测到“id”时出现错误,“id”才必须为空。因此,显然杰森甚至无法在请求中找到“id”。诡异的。假设 Jayson 没有问题,我猜这是一个引号转义问题,但我看不出该请求有什么问题。
    • 我唯一可以建议的另一件事是将它通过一个 linter 看看这是否会帮助你弄清楚,我注意到删除外部单引号将允许它传递 jsonlint.com 例如.
    • 是的,它应该工作,也根据 jsonlint.com。我现在已经在 GitHub 上向 Jayson 提交了一份错误报告,地址为:github.com/tedeh/jayson/issues/87。谢谢你的帮助,布赖恩 :)
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2019-02-07
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多