Skip to content

Instantly share code, notes, and snippets.

@nandana
Created February 1, 2017 16:37
Show Gist options
  • Save nandana/a5ec72f661183199d04234a334a7cf94 to your computer and use it in GitHub Desktop.
Save nandana/a5ec72f661183199d04234a334a7cf94 to your computer and use it in GitHub Desktop.
patent search client
//A simple node client that consume dummy patent search service.
var http = require('http');
//Search query parameters. Only two optional parameters are used.
var post_data = JSON.stringify( {
"keywords": [
"bluetooth", "beacon", "rfid"
],
"dateFrom": "20140101",
"dateTo": "20161231"
});
//Set the options for the POST request.
var options = {
hostname: 'dummy-patents.eu-gb.mybluemix.net',
port: 80,
path: '/patent-search',
method: 'POST',
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(post_data)
}
};
post_req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
var responseBody = '';
res.on('data', function (chunk) {
responseBody += chunk;
});
res.on('end', function () {
patentArray = JSON.parse(responseBody);
console.log('Got ' + patentArray.length + ' patents');
for(var i = 0; i < patentArray.length; i++) {
console.log('Patent ID' + patentArray[i].patentId);
console.log('Patent Title' + patentArray[i].title);
//Parse rest of the data as needed
}
});
});
post_req.on('error', function(e) {
console.log('A problem with request: ' + e.message);
});
//perform the request
post_req.write(post_data);
post_req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment