node.js http and bing search api - javascript

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
})

Related

Node.js http post request with basic authentication

I can make this work with axios but as I want to do this with default http module for some reasons
Here is the code
var express = require("express");
const http = require('https');
var app = express();
app.listen(3000, function () {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const data = JSON.stringify({
campaign_id: 'all',
start_date: '01/01/2010',
end_date: '05/31/2030',
return_type: 'caller_view',
criteria: {
phone: 98855964562
}
});
var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";
const options = {
hostName: hostName,
path: path,
port: 3000,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'Content-Length': data.length
}
};
const req = http.request(options, (res) => {
console.log('response is ' + res);
});
req.on('error', (error) => {
console.log('error is ' + error);
});
});
But it is throwing exception
connect ECONNREFUSED 127.0.0.1:443
It seems like you're providing the wrong options object (perhaps copied over from axios). The Node.js HTTP module takes host or hostname in options, while you're providing hostName.
Reference: https://nodejs.org/api/http.html#http_http_request_options_callback
I'm not entirely sure what you're trying to do. Any reason why you need your application to listen? I'm assuming the application you're posting to is hosted somewhere else, as you're attempting to listen to port 3000 while also making the request to an application on port 3000. If each application is on a different host, this should be fine. Nonetheless, you've at least got 3 issues here.
1) Your options object is incorrect. You're using hostName when it should be hostname. This is why you get the ECONNREFUSED 127.0.0.1:443 error; the options object for the https.request() method defaults hostname to localhost and port to 443.
2) Also, you never write your data object contents to the request stream.
3) Finally, you should listen to the data event to get the response back and write it to the console. I've updated your code as shown below:
var express = require("express");
const http = require('https');
var app = express();
app.listen(3000, function () {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const data = JSON.stringify({
campaign_id: 'all',
start_date: '01/01/2010',
end_date: '05/31/2030',
return_type: 'caller_view',
criteria: {
phone: 98855964562
}
});
var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";
const options = {
hostname: hostName,
path: path,
port: 3000,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'Content-Length': Buffer.byteLength(data)
}
};
const req = http.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (error) => {
console.log('error is ' + error);
});
req.write(data);
req.end();
});
You cannot move your express application to AWS Lambda as is. There are tools such as claudia which can help you move the app to lambda and api gateway.
In your case, you can modify your code AWS Lambda as below
const http = require('https');
exports.myHandler = function (event, context, callback) {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const data = JSON.stringify({
campaign_id: 'all',
start_date: '01/01/2010',
end_date: '05/31/2030',
return_type: 'caller_view',
criteria: {
phone: 98855964562
}
});
var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";
const options = {
hostName: hostName,
path: path,
port: 3000,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'Content-Length': data.length
}
};
const req = http.request(options, (res) => {
console.log('response is ' + res);
callback(null, res);
});
req.on('error', (error) => {
console.log('error is ' + error);
callback(error);
});
}
You have to invoke your lambda via API Gateway or via other AWS resources such as Alexa Skill Kit etc.
EDIT
You may try passing auth options as specified # https://github.com/request/request/blob/master/README.md#http-authentication

while sending data through post request using request module in nodejs got error

var http = require('http');
var querystring = require('querystring');
var request = require('request');
var postData = querystring.stringify({
msg: 'hello world'
});
var request = require('req')
var options = {
hostname: 'localhost',
port: 8000,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 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);
});
res.on('end', function() {
console.log('No more data in response.');
});
});
when i run this code I am getting cannot find 'req' module .I could see all the modules are properly installed in package.json and i did npm install too.Is there any problem in the code ?
Get rid of this line: var request = require('req')
You need to delete var request = require('req');

Request forbidden while accessing github api on node.js program

I'm trying to run this code but getting this error:
Request forbidden by administrative rules, make sure your request has a User-Agent header
var https = require("https");
var username = 'jquery';
var options = {
host: 'api.github.com',
path: '/users/' + username + '/repos',
method: 'GET'
};
var request = https.request(options, function(response){
var body = '';
response.on("data", function(chunk){
body += chunk.toString('utf8');
});
response.on("end", function(){
console.log("Body: ", body);
});
});
request.end();
Your options object does not have the headers option, describing the user-agent. Try this:
var options = {
host: 'api.github.com',
path: '/users/' + username + '/repos',
method: 'GET',
headers: {'user-agent': 'node.js'}
};

How to pass response paramaters including body from POST http.request in NodeJS

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){
...
});

In Node.js / Express, how do I "download" a page and gets its HTML?

Inside the code, I want to download "http://www.google.com" and store it in a string.
I know how to do that in urllib in python. But how do you do it in Node.JS + Express?
var util = require("util"),
http = require("http");
var options = {
host: "www.google.com",
port: 80,
path: "/"
};
var content = "";
var req = http.request(options, function(res) {
res.setEncoding("utf8");
res.on("data", function (chunk) {
content += chunk;
});
res.on("end", function () {
util.log(content);
});
});
req.end();
Using node.js you can just use the http.request method
http://nodejs.org/docs/v0.4.7/api/all.html#http.request
This method is built into node you just need to require http.
If you just want to do a GET, then you can use http.get
http://nodejs.org/docs/v0.4.7/api/all.html#http.get
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
(Example from node.js docs)
You could also use mikeal's request module
https://github.com/mikeal/request
Simple short and efficient code :)
var request = require("request");
request(
{ uri: "http://www.sitepoint.com" },
function(error, response, body) {
console.log(body);
}
);
doc link : https://github.com/request/request
Yo can try with axios
var axios = require('axios');
axios.get("http://www.sitepoint.com", {
headers: {
Referer: 'http://www.sitepoint.com',
'X-Requested-With': 'XMLHttpRequest'
}
}).then(function (response) {
console.log(response.data);
});

Categories

Resources