【问题标题】:How do I prevent superfluous quotation marks in my restify response body?如何防止我的 restify 响应正文中出现多余的引号?
【发布时间】:2015-03-20 02:48:48
【问题描述】:

我创建了一个简单的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。

我怎样才能去掉这些不需要的引号?

【问题讨论】:

  • 响应值好像自动转成JSON了。

标签: node.js restify


【解决方案1】:

经过一些实验,我发现我可以通过明确指定我的响应的 Content-Type 来消除引号:

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();
});

【讨论】:

    【解决方案2】:

    来自documentation,如果你不想神奇地将响应转换为JSON,你可以这样做

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

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多