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.
Related
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.
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.
I want to make multiple HTTP requests with different bodies each time, using loop or something similar. Currently I use the following code for a single request which works fine:
var http = require('http');
var post_req = null,
post_data = JSON.stringify(require('./resources/example.json'));
var post_options = {
hostname: 'example.lk',
port : '80',
path : '/example',
method : 'POST',
headers : {
'Content-Type': 'application/json',
'Authorization': 'Cucmlp1qdq9CfA'
}
};
post_req = http.request(post_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('Response: ', chunk);
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(post_data);
post_req.end();
How can I use this code to preform multiple calls?
You can use async to call several `http
var async = require('async');
var http = require('http');
var post_data = [ data1, data2, data2]; //array of data, you want to post
//asynchronously, loop over array of data, you want to push
async.each(post_data, function(data, callback){
var post_options = {
hostname: 'example.lk',
port : '80',
path : '/example',
method : 'POST',
headers : {
'Content-Type': 'application/json',
'Authorization': 'Cucmlp1qdq9CfA'
}
};
post_req = http.request(post_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('Response: ', chunk);
});
res.on('end', function () {
callback();
});
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(data); //posting data
post_req.end();
}, function(err){
console.log('All requests done!')
});
I am trying to establish alexa voice downchannel stream in nodejs using https but i am getting parse error. here is the code
var https = require('https');
var fs = require('fs');
//read the accesstoken from text file.
var accessToken = fs.readFileSync('accessToken.txt','utf8');
var options = {
host: 'avs-alexa-na.amazon.com',
path: '/v20160207/directives',
scheme : 'https',
method: 'GET',
headers: {'Authorization': 'Bearer ${accessToken}'}
};
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);
});
req.end();
when i am running the above code, i am getting error
problem with request: Parse Error
could you please help me where i am doing mistake.
I am trying to download an xml file so I can parse it in nodejs. Here is the test I built via mocha, the test passes but nothing is showing up on the console. What am I doing wrong?
it('Get XML data', function() {
var options = {
hostname: 'stona-app01.us.work.net',
port: 1213,
path: '/solrLive/STONA-STOUS-product-search-idx-en_US/select/?version=2.2&start=2&rows=2&qf=SKU&fl=*&indent=on&onlineFlag=1',
method: 'POST'
};
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);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});
The URL in the code is not reachable so you will not be able to test it.
Your test is asynchronous, so you must define your test to take a done parameter and call it when your test is done:
it('Get XML data', function (done) {
var options = {
hostname: 'stona-app01.us.work.net',
port: 1213,
path: '/solrLive/STONA-STOUS-product-search-idx-en_US/select/?version=2.2&start=2&rows=2&qf=SKU&fl=*&indent=on&onlineFlag=1',
method: 'POST'
};
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);
done(); // We're done!
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
done(e); // Pass the error to Mocha.
});
});