【发布时间】:2023-03-16 12:00:01
【问题描述】:
我试图弄清楚如何在“jayson”npm 包上使用 CURL 查询 nodejs 可从此处https://github.com/tedeh/jayson 获得。我在服务器端使用下面的测试程序。 Node 运行正常,并以错误输出响应 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
非常感谢您的帮助, 雷米
【问题讨论】: