【问题标题】:Node.js when sending POST request with base64 decoded characters, invalid characters error appearsNode.js 发送带有 base64 解码字符的 POST 请求时,出现无效字符错误
【发布时间】:2017-04-11 06:58:55
【问题描述】:

我正在尝试使用 base64 解码字符串发送 POST 请求,但是出现无效字符错误。

我有这个块来生成base64解码的字符串:

var domain = "domain.com";
var decoded = Buffer.from("x3abaARV"+domain+"1cYNYtk", 'base64').toString('ascii');

然后我使用来自 npm 的 request 模块发送 POST 请求:

var headers = {
    ip: "10.100.0.420",
    key: decoded,
    domain: domain
}
var options = {
      url: 'http://somedomain.com/foo/bar/example.json',
      method: 'POST',
      headers: headers,
  }

request(options, (error, response, body) => {
    if (error) {
      console.log(error);
    }
...

返回:

ypeError: 标头内容包含无效字符 在 ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:358:11) 在新的 ClientRequest (_http_client.js:86:14) 在 Object.exports.request (http.js:31:10) 在 Request.start (/home/kuno/code/aviokarte-web/node_modules/request/request.js:744:32) 在 Request.end (/home/kuno/code/aviokarte-web/node_modules/request/request.js:1435:10) 最后(/home/kuno/code/aviokarte-web/node_modules/request/request.js:566:14) 在立即。 (/home/kuno/code/aviokarte-web/node_modules/request/request.js:580:7) 在 runCallback (timers.js:637:20) 在 tryOnImmediate (timers.js:610:5) 在 processImmediate [as _immediateCallback] (timers.js:582:5)

如何正确设置?

【问题讨论】:

    标签: javascript node.js http post request


    【解决方案1】:

    如果您检查decoded 的值,您会发现HTTP 标头值中不允许出现不可打印的字符。您需要对此类数据使用某种二进制安全编码(例如 base64 或 URL/“百分比”编码)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多