0

When trying to hit always get an error with the help of the callback method but always getting error & trying to call API in a POST HTTP Method in NodeJS, tried all solutions but got nothing.

exports.createWallet = function(user_id, password, callback) {

var api_code = config.blockchain.api_code;
var user = user_id;
var pwd = password;

var result;

const post_data = JSON.stringify({
    'api_code': api_code,
    'password': pwd,
    'user_id': user
});

const options = {

    hostname: 'localhost',
    port: 8586,
    path: '/api/v2/create',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(post_data)
    }
};

var request = https.request(options, function (response) {
});

request.on('error', function (error) {

    console.log(error);
});

// With http.request() one must always call request.end() to signify the end of the request - even if there is no data being written to the request body.
request.end();
};
{ Error: socket hang up

    at TLSSocket.onHangUp (_tls_wrap.js:1120:19)
    at Object.onceWrapper (events.js:293:19)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNR
ESET' }
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Rohit Parihar
  • 352
  • 1
  • 7
  • 14

2 Answers2

0

In options, try adding body: post_data

  • const options = { hostname: 'localhost', port: 8586, path: '/api/v2/create?api_code'+api_code+'&password'+pwd+'&user_id'+user, method: 'POST', protocol: 'https:', rejectUnauthorized: true, strictSSL: false, body: post_data, headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(post_data) } }; – Rohit Parihar Apr 23 '18 at 05:23
  • add body: post_data, in options but getting same error again & again – Rohit Parihar Apr 23 '18 at 05:24
  • and also try other option but getting same "Socket hang up with ECONNRESET" – Rohit Parihar Apr 23 '18 at 05:26
  • One more thing, `'Content-Type': 'application/json'`. Apart from that, In your application, ensure whether your app expects any access_token or not. If so, whether it expect it as header or request url. Do this accordingly. And, Don't pass your post_data in request url. This is passed in body. – shubhambharti201 Apr 23 '18 at 10:13
  • const options = { hostname: 'localhost', port: 8586, path: '/api/v2/create?api_code='+api_code+'&password='+pwd+'&user_id='+user, method: 'POST', }; var request = http.request(options, function (response) { } – Rohit Parihar Apr 24 '18 at 20:31
  • working in all API's case without use https, just use HTTP. – Rohit Parihar Apr 24 '18 at 20:35
0

//Just use without https & pass parameters in path

var http = require("http");     
const options = {

    hostname: 'localhost',
    port: 8586,
    path: '/api/v2/create?api_code='+api_code+'&password='+pwd+'&user_id='+user,
    method: 'POST',
};

var request = http.request(options, function (response) {

}
Rohit Parihar
  • 352
  • 1
  • 7
  • 14