user1283776
user1283776

Reputation: 21764

API post request from gulp does not succeed

The following request does not succeed. I get a response, but not a success response. How can I fix this? I have gotten the request to work from postman, but not in 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();

This is the beginning of the response I get:

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,

Upvotes: 1

Views: 875

Answers (1)

andorx
andorx

Reputation: 349

Following these document for http.request, your options should be

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

You also have to use https instead of http for secured request and end the request by req.end()

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

req.end();

Upvotes: 2

Related Questions