在Node Js中执行发布请求时,获取“请求路径包含非转义字符错误”

时间:2016-12-01 08:06:17

标签: node.js request httpclient

尝试通过代理发出POST请求,在请求中收到类似非转义字符的错误。但是没有代理就行得很好。

var request = require('request');
request.post({
            url: "https://sezion.com/api?accountID=58340ff7c05c1c8dac59e895&accountSecret=XXXXXXXXXXXXXXXXXXXXX=",
            proxy: "http://proxy.example.com:8989",
            headers: {
                "Content-Type": "application/json"
            },
            body: '{"jsonrpc":"2.0","id":"zdoLXrB5IkwQzwV2wBoj","method":"barrister-idl","params":[]}',
            json:true
}, function(error, response, body){
console.log(error);
console.log(JSON.stringify(response));
  console.log(body);
});

运行此代码时,收到错误,

_http_client.js:53
    throw new TypeError('Request path contains unescaped characters');
    ^

TypeError: Request path contains unescaped characters
    at new ClientRequest (_http_client.js:53:11)
    at TunnelingAgent.exports.request (http.js:31:10)
    at TunnelingAgent.createSocket (D:\Seizon\node_modules\request\tunnel.js:117:25)
    at TunnelingAgent.createSecureSocket [as createSocket] (D:\Seizon\node_modules\request\tunnel.js:189:41)
    at TunnelingAgent.addRequest (D:\Seizon\node_modules\request\tunnel.js:81:8)
    at new ClientRequest (_http_client.js:158:16)
    at Object.exports.request (http.js:31:10)
    at Object.exports.request (https.js:199:15)
    at Request.start (D:\Seizon\node_modules\request\main.js:428:30)
    at Request.write (D:\Seizon\node_modules\request\main.js:812:28)

1 个答案:

答案 0 :(得分:0)

var request = require('request');
var qs = require('querystring');

var url = "https://sezion.com/api?" +
    qs.stringify({
        accountID: '58340ff7c05c1c8dac59e895',
        accountSecret: 'XXXXXXXXXXXXXXXXXx='
    });
request.post({
    url: url,
    headers: {
        "Content-Type": "application/json"
    },
    body: { "jsonrpc": "2.0", "id": "zdoLXrB5IkwQzwV2wBoj", "method": "barrister-idl", "params": [] },
    json: true
}, function (error, response, body) {
    console.log(error);
    console.log(JSON.stringify(response));
    console.log(body);
});