来自gulp的API post请求不成功

时间:2016-05-26 10:58:39

标签: node.js gulp

以下请求未成功。我得到了答复,但没有成功回应。我怎样才能解决这个问题?我已经得到了邮递员的工作请求,但没有一口气。

var proj = {
    "APIKey": "hidden",
    "APIPwd": "hidden",
    "Name": "Sample1",
    "ReplaceNames": true,
    "MoveStrings": true,
    "EncodeStrings": true,
    "items": [
        {
            "FileName": "test0.js",
            "FileCode": "function hello(x,y){var z=11;return x+y+z;}"
        },
        {
            "FileName": "test1.js",
            "FileCode": "var strs=['aaaa','bbbb','cccc','dddd','eeee','abcdefghijklmnopqrstuvwxyz0123456789']"
        }
    ]
};

var options = {
    hostname: 'https://service.javascriptobfuscator.com/HttpApi.ashx',
    method: 'POST',
    headers: {
        'Content-Type': 'text/json'
    }
};

var req = http.request(options, function(res) {
    console.log('Status: ' + res.statusCode);
    console.log('Headers: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (body) {
        console.log('Body: ' + body);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(JSON.stringify(proj));
req.end();

这是我得到的回应的开始:

problem with request: getaddrinfo ENOTFOUND https://service.javascriptobfuscator.com/HttpApi.ashx https://service.javascriptobfuscator.com/HttpApi.ashx:80
IncomingMessage {
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: true,
     ended: true,
     endEmitted: true,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,

1 个答案:

答案 0 :(得分:2)

根据http.request的这些文件,您的选项应为

var options = {
    hostname: 'service.javascriptobfuscator.com',
    path: '/HttpApi.ashx',
    method: 'POST',
    headers: {
        'Content-Type': 'text/json'
    }
};

您还必须使用https代替http来获取安全请求,并通过req.end()

结束请求
var https = require('https');
...
var req = https.request(options, function(res) {
    //
});

req.end();