使用json帖子的摘要身份验证的卷曲命令

时间:2013-03-21 15:45:46

标签: node.js post curl express digest-authentication

(使用node.js,express,passport-http)

我有一个POST路由做digest auth,尝试application-json内容类型。

我可以使用摘要点击GET路由,没有问题,我可以使用基本身份验证而无问题地点击POST路由,但是当我尝试使用摘要身份验证进行POST时,我得到400 - 错误请求。看起来像curl将内容类型放在初始摘要请求上(内容长度为0,所以它知道不能在初始digest-auth请求上发送json主体),而我的一方(express)失败了无效的json(空体):

$ curl  -v --digest  -X POST --data @body.json --user org2user2:lameduck -H "content-type: application/json"  http://127.0.0.1:3002/user

* About to connect() to 127.0.0.1 port 3002 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 3002 (#0)
* Server auth using Digest with user 'org2user2'
> POST /user HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: 127.0.0.1:3002
> Accept: */*
> content-type: application/json
> Content-Length: 0
> 
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< Content-Type: text/plain
< Date: Thu, 21 Mar 2013 15:33:10 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

我似乎无法弄清楚在没有这个的情况下发送摘要初始数据包的curl魔法,只在后面的实际数据请求中添加内容类型。

作为参考,虽然我觉得它没有帮助,但这是同一个电话的BASIC成绩单:

$ curl  -v --basic  -X POST --data @body.json --user org2user2:lameduck -H "content-type: application/json"  http://127.0.0.1:3002/user
* About to connect() to 127.0.0.1 port 3002 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 3002 (#0)
* Server auth using Basic with user 'org2user2'
> POST /user HTTP/1.1
> Authorization: Basic b3JnMnVzZXIyOmxhbWVkdWNr
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: 127.0.0.1:3002
> Accept: */*
> content-type: application/json
> Content-Length: 48
> 
* upload completely sent off: 48 out of 48 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 51
< Date: Thu, 21 Mar 2013 15:43:48 GMT
< Connection: keep-alive
< 
{
  "this": "is working",
  "that": "is annoying"
* Connection #0 to host 127.0.0.1 left intact
}* Closing connection #0

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我无法回答你关于命令行魔术的问题,告诉CURL不要在初始请求上发送内容类型(我认为没有任何魔法)。

但是,我可以告诉你,问题的根本原因是Node + Express(connect)通过bodyParser发送初始摘要请求,并且因为application / json标头在那里它试图解析正文(是空的)。就个人而言,我认为如果身体是空的,快递应该不会发生,而只是返回一个空的JSON结构(我的工作在下面)。

将来可能有一个更好的(官方)解决方法,因为这个特殊问题现在正在github上进行讨论(1天) https://github.com/senchalabs/connect/issues/415

我的解决方法(connect / lib / middleware / json.js:70)

if (0 == buf.length) {
//  return next(400, 'invalid json, empty body');
    req.body = {};
    return next();
}
相关问题