im trying to send multiple http put request to my server, but i only manage to send one JSON to the database, what i am missing here
var data1 = JSON.stringify(require('./abc.json')),
data2 = JSON.stringify(require('./abe.json')),
data3 = JSON.stringify(require('./abd.json'));
var put_data = [data1,data2,data3];
async.each(put_data, function(data, callback){
var post_options = {
hostname: config.serviceHost,
port : '80',
path : '/API/Nag',
method : 'PUT',
headers : {
'Content-Type': 'application/json',
'Authorization': config.secret
}
};
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!')
});
even tho it send 3 http request, all three request contains same data
The variable post_req has global scope. Meaning the value of post_req can be accessed and modified outside async.each function.
The solution is to add one var before post_req that would restrict the variable scope to async and also spawns a new variable for each iteration.
Related
I am calling below url with get https request using Node.js, here i need to send the session with cookie, since i know the vallue of cookie.
var URL = require('url-parse');
var appurl = "https://test.somedomain.com"
var https = require('https');
var url = new URL(appurl);
var options = {
host: url.host,
url: appurl,
path: url.pathname + url.query,
method: 'GET',
headers: {
"Set-Cookie": "cookiValue1=abcdesg"
}
};
var req = https.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
// console.log(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.write('data\n');
req.write('data\n');
req.end();
The header should be Cookie instead of Set-Cookie
Set-Cookie is used by the server to set a cookie on clients. Header Cookie is used to send a cookie to server by client.
So nodejs server it's not a browser which have window and document
try to send the same request in js client side not nodejs
You can set cookie only here
see POINTERs in the bottom
var URL = require('url-parse');
var https = require('https');
var appurl = "https://stackoverflow.com/"
var url = new URL(appurl);
var options = {
host: url.host,
url: appurl,
path: url.pathname + url.query,
method: 'GET',
headers: {
"Cookie": "1111111111=abcdesg", // POINTER its unfortunately useless
"Set-Cookie": "11111111=abcdesg" // POINTER its unfortunately useless
}
};
var req = https.request(options, function (res) {
console.log('before HEADERS: ' + JSON.stringify(res.headers));
res.headers.cookiValue1 = 'abcdesg'; // POINTER
console.log('after HEADERS: ' + JSON.stringify(res.headers));
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write('data\n');
req.write('data\n');
req.end();
I want to make a REST call and get the result out in a variable (access_token). My variable AFAIK is global. Why is my variable access_token undefined at the end, even though i get a result in the console ? I understand that this call is async, but I put a 'false' in the call.
var https = require('https'),
querystring = require('querystring');
require('request-to-curl');
// First, get access token
var postData = querystring.stringify({
'grant_type': 'password',
'username':'<myusername>',
'password':'<mypassword>'
});
var options = {
hostname: 'myserver.com',
port: 443,
path: '/v1/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
function postVal(options, value) {
//var responseObject;
var access_token;
var responseObject;
var req = https.request(options, function(res)
{
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
console.log(responseString);
responseObject = (JSON.parse(responseString));
access_token = responseObject.access_token;
console.log('console access:' + access_token);
value(access_token);
// return responseObject;
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
}
console.log("Access:"+ postVal(options, function(access) {
console.log(access);
}));
Result:
$ node curl.js
Access:undefined
{"token_type":"Bearer","access_token":"f3DzDqW..dbMo0","refresh_token":"4jXqo4..kuuc2rMux3","scope":"global","access_token_type":"USER_GENERATED","note":"","expires_in":900}
console access:f3DzDqWpnrgxnxt5vE42ih8ew..gOKyJY5dbMo0
mlieber-ltm12:
I am trying to use the Bing Search API to return a JSON string. I first tried using the following url as per Azure's explore website (https://datamarket.azure.com/dataset/explore/5BA839F1-12CE-4CCE-BF57-A49D98D29A44):
'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27NGI%20SPA%27&Market=%27en-US%27'
After, I found a SO thread Using the new Bing API (nodejs) which suggested I use a url of the form:
https://user:<YourDefaultAccountKey>#api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27leo%20fender%27&Market=%27en-US%27&$top=50&$format=JSON
Both of these return status 401 (Authentication Failure):
STATUS: 401
HEADERS: {"content-type":"application/json; charset=utf-8","server":"Microsoft-IIS/8.0","jsonerror":"true","x-powered-by":"ASP.NET","access-control-allow-origin":"*","access-control-allow-credentials":"false","access-control-allow-headers":"Authorization, DataServiceVersion, MaxDataServiceVersion","access-control-expose-headers":"DataServiceVersion, MaxDataServiceVersion","access-control-allow-methods":"GET, POST, OPTIONS","access-control-max-age":"604800","date":"Wed, 02 Jul 2014 17:23:29 GMT","content-length":"91"}
BODY: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
I have also tried other various combinations of URLs to no avail. My code is below:
var url = require('url');
var http = require('http');
var serviceRootURL = 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27NGI%20SPA%27&Market=%27en-US%27'
var params = 'hi';
var dataURL = url.parse(serviceRootURL);
var post_options = {
hostname: dataURL.hostname,
port: dataURL.port || 80,
path: dataURL.path,
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': params.length
}
};
var 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('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();
Any idea why I am getting an authentication failure?
You can use this module that encapsulates the requests, so you can use it
like:
var Bing = require('node-bing-api')({ accKey: "your-account-key" });
Bing.web("leo fender", function(error, res, body){
console.log(body);
},
{
top: 50,
market: 'en-US'
});
It works with the Azure version. You only have to replace your account key.
Got it working with request...weird
var request = require('request');
var _ = require('underscore');
var searchURL = 'https://user:<TIPE YOUR KEE HEER>#api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$top=10&$format=JSON';
var http = request( searchURL, function(err, resp, body)
{
if ( err )
{
throw err;
}
var a = JSON.parse(body);
console.log(a.d.results);
});
you can use jsearch module. install ;
npm install jsearch
usage;
js.bing('queryStringYouWant',10,function(response){
console.log(response) // for Bing results
})
This http.request code is from http://nodejs.org/docs/v0.4.7/api/http.html#http.request.
How to export chunk in res.on ?
var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
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);
});
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
I'm not sure what you mean by "export" but perhaps you'd like to put the contents of the response into a local text file?
Here's how you might go about doing that:
var http = require('http');
var fs = require('fs');
var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
var response;
if(fs.existsSync('response.html'))
response = fs.readFileSync('response.html') + chunk;
else
response = chunk;
fs.writeFileSync('response.html', response);
});
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
Note that after each data event is fired, we're checking for an existing file with fs.existsSync, populating a response variable accordingly and then writing the response to a file again with fs.writeFileSync.
This wouldn't be much use on a server, as the synchronous nature of the file reads/writes would bottleneck your traffic, but it does highlight the general concept of responding to events and concatenating chunks.
I am trying to write a basic REST Post client to work with node.js and because of the REST API I have to work with I have to get details from the responses including cookies to maintain the state of my REST session with the server. My Question is what is the best way to pull the json objects from the response when res.on triggers with all the data in the PRINTME variable and return it to the test.js console.log().
test.js file
var rest = require('./rest');
rest.request('http','google.com','/upload','data\n');
console.log('PRINTME='JSON.stringify(res.PRINTME));
rest.js module
exports.request = function (protocol, host, path, data, cookie){
var protocalTypes = {
http: {
module: require('http')
, port: '80'
}
, https: {
module: require('https')
, port: '443'
}
};
var protocolModule = protocalTypes[protocol].module;
var options = {
host: host,
port: protocalTypes[protocol].port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'text/xml'
, 'Content-Length': Buffer.byteLength(data)
, 'Cookie': cookie||''
}
};
console.log('cookies sent= '+options.headers.Cookie)
var req = protocolModule.request(options, function(res) {
var PRINTME = res;
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
PRINTME.body = chunk;
console.log('BODY: ' + chunk);
});
res.on('close', function () {res.emit('end')});
});
req.on('error', function(e) {
console.error('Request Failure: ' + e.message);
});
req.write(data);
req.end();
};
Using a package like request will help you simplify your code.
The following would be rest.js
var request = require('request');
module.exports = function(protocol, host, path, data, cookie, done) {
var options = {
host: host,
port: protocalTypes[protocol].port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'text/xml',
'Content-Length': Buffer.byteLength(data)
},
jar: true
};
request(options, function(err, resp, body) {
if (err) return done(err);
// call done, with first value being null to specify no errors occured
return done(null, resp, body);
});
}
Setting jar to true will remember cookies for future use.
See this link for more information on the available options
https://github.com/mikeal/request#requestoptions-callback
To use this function in another file
var rest = require('./rest');
rest(... , function(err, resp, body){
...
});