NodeJS request-json设置标头中断

时间:2015-07-06 20:11:16

标签: json node.js

我正在使用在npm上找到的request-json来查询我的API服务器。我的服务器要求在标头中传递一个auth令牌,但是使用request-json设置头的方法会产生以下错误:

Error: "name" and "value" are required for setHeader().
at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:333:11)
at new ClientRequest (_http_client.js:101:14)
at Object.exports.request (http.js:49:10)
at Request.start (C:\Users\Michael\Desktop\FrescoWeb\node_modules\request-js
on\node_modules\request\request.js:904:30)
at Request.write (C:\Users\Michael\Desktop\FrescoWeb\node_modules\request-js
on\node_modules\request\request.js:1625:10)
at end (C:\Users\Michael\Desktop\FrescoWeb\node_modules\request-json\node_mo
dules\request\request.js:666:16)
at Immediate._onImmediate (C:\Users\Michael\Desktop\FrescoWeb\node_modules\r
equest-json\node_modules\request\request.js:690:7)
at processImmediate [as _immediateCallback] (timers.js:358:17)

我正在设置标题并在以下代码中点击端点:

var api = requestJson.createClient(config.API_URL);
api.headers['authtoken'] = req.session.token;
api.post(
'/v1/outlet/update',
  params,
  function(error, response, body){
    if (error)
      return res.json({err: error}).end();
    if (!body)
      return res.json({err: 'ERR_MISSING_BODY'}).end();
    if (body.err)
      return res.json({err: body.err}).end();

    req.session.user.outlet = body.data;
    req.session.save(function(){
      res.json({}).end();
    });
  }
);

当我评论api.headers['authtoken'] = req.session.token;时,通话不会崩溃。是否存在我做错的事情,或者我是否必须迁移到request以获取http请求?

1 个答案:

答案 0 :(得分:0)

req.session.token应为req.session.user.token