如何在我的restify响应体中防止多余的引号?

时间:2015-03-19 18:43:32

标签: node.js restify

我创建了一个简单的restify服务器,我试图在我的响应主体中返回一个字符串:

server.post('/authurl', function authurl(req, res, next) {
    res.send(dhs.getAuthorizeUrl());
    return next();
});

但是,我注意到响应正文被双引号包围,我没有要求:

"https://some.url.com/oauth/v4/authorize?client_id=someid&scope=SOMESCOPE"

我已经验证这些额外的引号是而不是来自getAuthorizeUrl方法 - 它会返回裸URL。

如何摆脱这些不需要的引号?

2 个答案:

答案 0 :(得分:4)

经过一些实验,我发现我可以通过明确指定我的回复的内容类型来消除引号:

server.post('/authurl', function authurl(req, res, next) {
    /*jslint unparam: true*/
    res.contentType = "text/plain"; // needed so the platform doesn't add superfluous quotation marks around the URL in the response body
    res.send(dhs.getAuthorizeUrl());
    return next();
});

答案 1 :(得分:2)

documentation开始,如果您不想将响应神奇地转换为JSON,则可以

var body = 'hello world';
res.writeHead(200, {
  'Content-Length': Buffer.byteLength(body),
  'Content-Type': 'text/plain'
});
res.write(body);
res.end();
相关问题