getaddrinfo ENOTFOUND when making a post request using node.js - node.js

I want to send a POST request to my server using Node.js
function login(email, company_code, password){
var options = {
hostname: '266fd57b.ngrok.io',
path: '/v1/manager/sign_in',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
'email': email,
'company_code': company_code,
'password': password
}
};
var req = https.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('{"string": "Hello, World"}');
req.end();
return true;
}
But I keep getting error:
getaddrinfo ENOTFOUND 266fd57b.ngrok.io 266fd57b.ngrok.io:443
I tried all the fixes found on the Internet but nothing worked.

Oh my god this is because I have not entered billing account for google.
Dialogflow needs a credit card number to work.

Related

NodeJS 12.x https version of a curl request on AWS Lambda

curl -d "m_payment_id=m_payment_id-xxxxxxxxxxxxxxxxxyyy&pf_payment_id=990396&payment_status=COMPLETE&item_name=Subscription&item_description=Monthly+Subscription&amount_gross=99.00&amount_fee=-6.74&amount_net=92.26&custom_str1=&custom_str2=&custom_str3=&custom_str4=&custom_str5=&custom_int1=&custom_int2=&custom_int3=&custom_int4=&custom_int5=&name_first=&name_last=&email_address=christo%40g4-ape.co.za&merchant_id=0000000&token=0000000-0000-0000-3a83-25bc733a307b&billing_date=2020-02-21&signature=3895d0769b56862b842da5067af4483f" -X POST https://sandbox.somedomain.co.za/what/something/validate
My attempt:
const https = require("https");
const querystring = "m_payment_id=m_payment_id-xxxxxxxxxxxxxxxxxyyy&pf_payment_id=990396&payment_status=COMPLETE&item_name=Subscription&item_description=Monthly+Subscription&amount_gross=99.00&amount_fee=-6.74&amount_net=92.26&custom_str1=&custom_str2=&custom_str3=&custom_str4=&custom_str5=&custom_int1=&custom_int2=&custom_int3=&custom_int4=&custom_int5=&name_first=&name_last=&email_address=christo%40g4-ape.co.za&merchant_id=0000000&token=0000000-0000-0000-3a83-25bc733a307b&billing_date=2020-02-21&signature=3895d0769b56862b842da5067af4483f";
return new Promise((resolve, reject) => {
const options = {
hostname: 'sandbox.somedomain.co.za',
port: 443,
path: 'what/something/validate',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(querystring)
}
};
const req = https.request(options, (res) => {
console.log('statusCode: ' + res.statusCode);
console.log('headers: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('BODY: ' + data);
});
resolve('Success');
});
req.on('error', (e) => {
console.log('problem with request: ' + e.message);
reject(e.message);
});
// write data to request body
req.write(querystring);
req.end();
});
I keep getting a statusCode 400 on the NodeJS code, the curl works fine. The hostname hs obviously been changed for security.
Can someone please advise me on what I'm doing wrong here?
Content-Length is reqired when you are sending the body while posting. In your case all the info is getting passed as query string parameter so you can get rid of the headers alltogather.
Also, you should be resolving inside res.on('end'. THe way you are doing it will finish the function before it even finishes execution.

How can I make an HTTPS POST request in node.js?

How can I make an HTTPS POST request to an API. My code doesn't work, because it cannot find the resource.
API Documentation: https://applymagicsauce.com/documentation at Authentication
My code, which doesn't work
var https = require('https');
var options = {
host: 'https://api.applymagicsauce.com',
port: 443,
path: '/auth',
method: 'POST'
};
var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
error message: STATUS: 415
HEADERS: {"server":"nginx/1.15.10","date":"Fri, 09 Aug 2019 18:22:48 GMT","content-type":"application/json;charset=UTF-8","transfer-encoding":"chunked","connection":"close","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","cache-control":"no-cache, no-store, max-age=0, must-revalidate","pragma":"no-cache","expires":"0","strict-transport-security":"max-age=15724800; includeSubDomains","x-frame-options":"DENY"}
BODY: {"timestamp":"2019-08-09T18:22:48.981+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/octet-stream' not supported","path":"/auth"}
Looks like your host includes a path, but you're also providing a path.

Node.js response truncated

I am getting truncated response in my node service.
I am using http request.Please notify me any error in this code.
I want to get response without losing data.
My Code :
var config = require('./configuration')
exports.postDataDetails = function postDataDetails(http,url,port,path,inputParam,callback){
var postData = inputParam;
var options = {
hostname: url+":",
port: port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + new Buffer(config.headerUsername + ':' + config.headerPassword).toString('base64'),
'Content-Length': Buffer.byteLength(postData,'utf8')//postData.length
}
};
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 (chunk) {
console.log('BODY: ' + chunk);
return callback(chunk);
});
res.on('end', function() {
console.log('No more data in response.');
return callback(JSON.stringify({status : "No more data in response."}));
})
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
return callback(JSON.stringify({status : "error"}));
});
// write data to request body
req.write(postData);
req.end();
}
How to fix that problem.
Thanks.

nodejs paypal pdt return 302

I'm using nodejs and express. This is my code that is run on the return from Paypal. I only get a 302 errors in response from Paypal. I saw a couple examples that use ssl:// instead of https:// but nodejs yells saying that its not a valid protocol for the https module. Does anyone have a working nodejs script for PDT and IPN?
var purchaseID = req.query.tx;
var atoken = MYAuthToken;
var postDataArray = {'cmd':'_notify-synch','tx': purchaseID, 'at': atoken}
var postData = JSON.stringify(postDataArray);
console.log(postData);
var options = {
hostname: 'www.sandbox.paypal.com',
port: 443,
path: '/cgi-bin/webscr',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = https.request(options, function(res) {
console.log('STATUS: '+ res.statusCode);
console.log('HEADERS: '+ JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('BODY: '+chunk);
});
res.on('end', function() {
console.log('No more data in response.')
});
});
req.on('error', function(e) {
console.log('problem with request: '+e.message);
});
req.write(postData);
req.end();
});
This
You're missing Accept: */* header. Also, JSON.stringify is not application/x-www-form-urlencoded. Here is some working code for you to build based on:
var request = require('request');
var endpoint = 'www.sandbox.paypal.com';
var options = {
form: {
cmd: '_notify-synch',
tx: tx,
at: auth
},
headers: {
Accept: '*/*'
}
};
request.post('https://' + endpoint + '/cgi-bin/webscr', options, function(e, r, body) {
return console.log(body);
});
Try just posting without the JSON
var postData = "cmd=_notify-synch,at=" + at + ",tx=" + tx;
I've edited a couple of times as i ran into issues. I'm new to node so just hacking out a solution with trial and error. Your post moved me towards the solution. So here is postData that works with your code. It's nice to see the FAIL ans SUCCESS messages come through. Note .. need the &'s
var postData = "cmd=_notify-synch&at=" + at + "&tx=" + tx;

node js http post request save cookie

I am calling an http post request(login request from another 3rd party server) from node and want to save the cookie for future http requests.
var options = {
host: 'hostname.com',
port: '80',
path: '/login.php',
method: 'POST'
};
var data = qs.stringify({
uname: "validUsername",
password: "validPassword"
});
var req = http.request(options, function(response) {
console.log(response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
// console.log('Cookies: ' + response.getHeader("Set-Cookie"));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(data);
req.end();
How can i access and save the cookie from the response sent by the requested server?
You should be able to get the header with response.headers['set-cookie'];.
If you need to actually parse them, you could use a module like cookie (what Express uses) or cookiejar.

Resources