Node.js 如何防止restify响应体中出现多余的引号?
我已经创建了一个简单的Node.js 如何防止restify响应体中出现多余的引号?,node.js,restify,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&s
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
如何去除这些不需要的引号?经过一点实验后,我发现可以通过明确指定响应的内容类型来消除引号:
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();
});
经过一点实验,我发现可以通过明确指定响应的内容类型来消除引号:
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();
});
如果您不想神奇地将响应转换为JSON,您可以这样做
var body = 'hello world';
res.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain'
});
res.write(body);
res.end();
如果您不想神奇地将响应转换为JSON,您可以这样做
var body = 'hello world';
res.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain'
});
res.write(body);
res.end();
响应值似乎自动转换为JSON。响应值似乎自动转换为JSON。