如何防止restify响应正文中多余的引号?
我创build了一个简单的restify
服务器,我试图返回一个string在我的响应正文:
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"
我已经validation了这些额外的引号不是来自getAuthorizeUrl
方法 – 它返回的是裸露的URL。
我怎样才能摆脱这些不需要的引号?
经过一些实验后,我发现我可以通过明确指定我的响应的内容types来消除引号:
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();