如何用curl查询jayson(nodejs)

时间:2014-03-25 17:07:54

标签: node.js curl

我试图找出如何使用CURL在“jayson”npm包中查询可从此处https://github.com/tedeh/jayson获得的nodejs。我在服务器端使用下面的测试程序。节点正常运行并响应Curl并输出错误。无法找到Curl中应传递的内容或节点程序应该更改的内容:

var jayson = require(__dirname + '/../..');
var server = jayson.server({
  echo: function(msg, callback) {
    if(msg != null)
      callback(null, msg);
  },
  add: function(a, b, callback) {
    if( (a!= null) && (b!= null) )
      callback(null, a + b);
  }
});
server.http().listen(90);

这是CURL命令:

$ curl -v -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "id":"curltest", "method":"echo", "params": ["hello"] }' http://localhost:90

如果curl命令不符合jayson,这就是Curl收到的nodejs答案:

* About to connect() to localhost port 90 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 90 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.28.1
> Host: localhost:90
> Accept: */*
> Content-Type: application/json
> Content-Length: 16
>
* upload completely sent off: 16 out of 16 bytes
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Content-Length: 79
Content-Length: 79
< Content-Type: application/json
Content-Type: application/json
< Date: Tue, 25 Mar 2014 19:26:21 GMT
Date: Tue, 25 Mar 2014 19:26:21 GMT
< Connection: keep-alive
Connection: keep-alive

<
* Connection #0 to host localhost left intact
{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}}* Closing connection #0

非常感谢你的帮助, 雷米

2 个答案:

答案 0 :(得分:0)

您正在从Windows运行curl。这就是为什么围绕这些参数的单引号对你不起作用的原因。将它们更改为双引号:

curl -i -X POST -H "Content-Type: application/json" -d "{\"echo\": \"Name\"}" http://localhost:90/?

此外,尝试运行最后追加-v的命令。它将显示curl的调试信息。如果它不适合你,请告诉我们msg。

答案 1 :(得分:0)

我发现了这个技巧,我纠正了上面的错误。我们应该是jsonrpc 2.0兼容的,因为Jayson只考虑jsonrpc2。

这是&#34; echo&#34;的正确curl命令行。脚本:

$ curl -v -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "id":"curltest", "method":"echo", "params": ["hello"] }' localhost:90 

这是&#34;添加&#34;的正确curl命令行。脚本:

$ curl -v -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "id":"curltest", "method":"add", "params": [1, 2] }' http://localhost:90