将Curl与Json(Node.js)一起使用
当我在将Curl与Json(Node.js)一起使用,node.js,curl,json-rpc,Node.js,Curl,Json Rpc,当我在curl中省略Json-RPC请求参数的“[和]”部分时,我很难让curl和Json(Node.js)正常工作。我使用的是命名参数(即对象,而不是数组) 这项工作: curl -v -i POST -H "Content-Type: application/json" \ -d '{"jsonrpc": "2.0", "method":"registerPerson", "params": ["{\"username\":\"morten10\",\"password\":\"mypass
curl
中省略Json-RPC
请求参数的“[和]”部分时,我很难让curl
和Json
(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
但是,当我从参数中省略[“and]”时,它不起作用:
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
但是,根据规范及其示例,当我使用命名参数时,我应该能够从参数中排除“[和]”部分,如本例所示:
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}
-->{“jsonrpc”:“2.0”,“方法”:“减法”,“参数”:{“分钟数”:42,“减数”:23},“id”:4}
我认为您的对象需要是错误提示的字符串
尝试:
谢谢,但我已经试过了。但是更改为:curl-v-ipost-H“内容类型:application/json”-d'{“jsonrpc”:“2.0”,“方法”:“registerPerson”,“参数”:“{“username\”:“morten10\”,“password\”:“mypass\”,“id”:1}”http://localhost:3000
使Jayson(在到达我的代码之前)发出以下消息:{“jsonrpc”:“2.0”,“id”:null,“error”:{“code”:-32600,“message”:“Invalid request”}
Go figure。有趣的是,Jayson在该错误响应中将“id”设置为null。根据JSON-RPC 2.0规范,“id”必须为null,只有在检测请求中的“id”时才会出现错误。因此,显然Jayson甚至找不到“id”“在请求中。奇怪的假设Jayson不是buggy,我猜这是一个引号转义问题,但我看不出该请求中有什么错误。我唯一能建议的另一件事是把它放在一个过梁上,看看这是否能帮助你找到答案,我注意到移除外部的单引号将允许它传递。例如,是的,它应该可以工作,另据jsonlint.com报道。我现在已经在GitHub上向Jayson提交了一份bug报告,在这里:。谢谢你的帮助,布莱恩:)
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