Node request module - sample get request failing - javascript

Vaguely following this 'tutorial': https://stackabuse.com/the-node-js-request-module/
I get to the alert ('Options Declared'), but then my program stops at the request and I have no idea why.
const request = require('request');
function testGet(){
alert("Get test started");
const options = {
url: "https://www.reddit.com/r/funny.json",
method: 'GET',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client'
}
}
alert("Options declared");
request(options, function(err, res, body){
let json = JSON.parse(body);
console.log(json);
});
alert("Get test done");
}

Just send the response and error as the response object and look at what comes.
const request = require('request');
function testGet(){
alert("Get test started");
const options = {
url: "https://www.reddit.com/r/funny.json",
method: 'GET',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client'
}
}
alert("Options declared");
request(options, function(err, res, body){
res.json({err : err, result: body});
});
}

You place the request function declaration and body inside testGet().
Please do that outside the testGet() like this.
function testGet(){
alert("Get test started");
const options = {
url: "https://www.reddit.com/r/funny.json",
method: 'GET',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client'
}
}
alert("Options declared");
request.post(options, callback);
}
request(options, function(err, res, body) {
let json = JSON.parse(body);
console.log(json);
alert("Get test done");
});

Related

HTTPS Node JS POST issue

I'm using https for this POST request.
Could someone point out the issue here? I get undefined and no error message .
I tried wrapping the request before res.write with no success. I'm getting a
statusCode: 400
Bad Payload received by generic incoming webhook.
Thanks!
connector.js
const https = require('https');
var dataString = {"text": "Hello World"};
const deets = {
portal_trade_events: {
host: "outlook.office.com",
path: "/path",
headers: {
'Content-Type': 'application/json',
'Content-Length': dataString.text.length
}
}
};
const options = {
hostname: deets.portal_trade_events.host,
port: 443,
path: deets.portal_trade_events.path,
method: 'POST',
headers: deets.portal_trade_events.headers
};
function post(options, data) {
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d)
});
});
req.on('error', (error) => {
console.error(error)
});
req.write(JSON.stringify(data));
req.end()
}
post(options, dataString);
At the very least, your headers for content-length look wrong. You're sending the full JSON, not just the one text value.
const https = require('https');
const data = JSON.stringify({
text: 'Hello World'
});
const options = {
hostname: 'outlook.office.com',
port: 443,
path: '/path',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (error) => {
console.error(error);
});
req.write(data);
req.end();

Express js post multipart form data to external API

I have an angular application and express server a middle ware which call the external API
$scope.uploadLayout = function(){
$scope.selectedTemplate.selected = $scope.items;
var fd = new FormData()
fd.append('name', $scope.template.name)
fd.append('description', $scope.template.desc)
fd.append('file', $scope.template.file)
$http.post('/layout/template',fd,{
transformRequest: angular.identity,
headers: { 'Content-Type': undefined}
}).then(function(response){
getLayoutRules()
console.log("file uploaded successfully")
})//add error callback
console.log('test')
}
The main problem occurs when express sends the data to the external API
app.post('/layout/template', upload.any(), (req, res) => {
// console.log(req.body.name);
var formdata = new Buffer(req.files)
var form = new FormData();
form.append('name', req.body.name)
form.append('description', req.body.description)
form.append('file', formdata)
console.log(form)
var contentLength = req.files.length;
var url = `${apiUrl}/layout/template`
// var r = request.post(url,{
// headers: { 'Content-Type': undefined}
// }, function optionalCallback(err, httpResponse, body) {
// if (err) {
// return console.error('upload failed:', err);
// }
// console.log('Upload successful! Server responded with:', body);
// })
request({
headers: {
'Content-Length': contentLength,
'Content-Type': 'multipart/form-data'
},
url:url,
method:'POST',
body: form
},function(err,body,res){
console.log(err)
console.log(body)
})
});
I have checked the form is generating properly but the external API server gives me an error saying that
"The request was rejected because no multipart boundary was found"
Kindly help, or is there any other way round it
UPDATE
I modified the code a bit to send the request headers fetched from the browser
app.post('/layout/template', upload.any(), (req, res) => {
var header = req.headers['content-type']
var formdata = new Buffer(req.files)
console.log(formdata)
var fd = new FormData();
fd.append('name', req.body.name)
fd.append('description', req.body.description)
fd.append('file', formdata)
//var contentLength = req.files.length;
//console.log(contentLength)
var url = `${apiUrl}/layout/template`
//console.log(fd)
// var r = request.post(url,{
// headers: { 'Content-Type': undefined}
// }, function optionalCallback(err, httpResponse, body) {
// if (err) {
// return console.error('upload failed:', err);
// }
// console.log('Upload successful! Server responded with:', body);
// })
request({
headers: {
'Content-Type': header
},
url:url,
method:'POST',
body: fd
},function(err,body,res){
//console.log(body)
console.log(res)
})
//console.log(request)
});
So now i am sending the same boundary set by browser but now the form parameters are not detected by java server

Consuming restfull api and share the result to web page using Node.js

I am trying to request data from RESTful API and sharing the result to html web page using Node.js. My code runs well, but I want to make this RESTful request every time I call the webpage not just when I run a Node.js server.
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var request = require("request");
var temp = "";
var options = { method: 'GET',
url: 'My_URL',
headers: { authorization: 'Basic My_Autho' }
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
temp = body;
console.log(body);
});
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'
});
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var renderedHtml = ejs.render(content, {temp: temp});
res.end(renderedHtml);
});
}).listen(8000);
Maybe you could move your call to the external REST api inside the request handler ?
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var request = require("request");
var temp = "";
var options = { method: 'GET',
url: 'My_URL',
headers: { authorization: 'Basic My_Autho' }
};
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
request(options, function (error, response, body) {
if (error) throw new Error(error);
temp = body;
console.log(body);
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var renderedHtml = ejs.render(content, {temp: temp});
res.end(renderedHtml);
});
});
}).listen(8000);

node js error on response methods between server and client

Hey im currently working on my first real webpage using node.js
Im using Express, ejs for layout and redis as database. Im trying to send an ajax call from my index page through my client to my server, use the ajax-call there and pass the final json back to my client where i try to render it on the next ejs page.
Ajax:
$(function(){
$(".search").click(function() {
$.ajax({
method: "POST",
url: "/search",
cache: false,
data: {ort: "hierundda", activity: "Wandern", datum: "01.09.2015"},
dataType: "json",
success: function(data){
alert('Success!')
}
, error: function(jqXHR, textStatus, err){
alert('text status '+textStatus+', err '+err)
}
});
});
});
My server route:
rest.post("/search", jsonParser, function(req, res){
/*some database action with redis */
res.json(dataJson);
});
});
My client route:
app.post('/search', jsonParser, function(req,res){
var test = JSON.stringify(req.body);
fs.readFile('./filterergebnis.ejs', {encoding: 'utf-8'}, function(err, filestring){
if(err){
throw err;
}
else{
var options = {
host: 'localhost',
port: 3000,
path: '/search',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': test.length
}
}
var req = http.request(options, function(res) {
res.on('data', function (chunk) {
var userdata = JSON.parse(chunk);
console.log(userdata);
var html = ejs.render(filestring, userdata);
//here does the error appear...
res.setHeader('content-type', 'text/html');
res.writeHead(200);
res.write(html);
res.end();
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(test);
req.end();
}
});
});
This is what the error looks like:
http://www.pic-upload.de/view-28225954/stack.png.html
index.ejs is running on default
You're using conflicting res variable names. Check the variable names of the callbacks from app.post() and http.request().
If you change to response instead, it might work, if there is no other problems:
var req = http.request(options, function(response) {
response.on('data', function (chunk) {
...

Http get multiple json files from different API endpoints using node express

I'm looking into the most efficient way to get multiple JSON files from different API endpoints using node.
Basically i'd like to store each JSON object in a variable, and send them all to Jade template files for parsing.
I've got it setup working for getting one single JSON file (jsonFile1) by doing the following:
httpOptions = {
host: 'api.test123.com',
path : '/content/food/?api_key=1231241412',
headers: {
"Accept": "application/json",
'Content-Type': 'application/json'
},
method: "GET",
port: 80
}
var jsonFile1;
http.get(httpOptions, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
jsonFile1= JSON.parse(body)
console.log("Got response: " + jsonFile1);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
app.set('views', __dirname);
app.get('/', function(req, res) {
res.render('home', {
data: jsonFile1
});
});
But i don't really want to have to repeat all of this to get multiple json endpoints and send them to home jade template.
Any ideas to do this efficiently?
Based on your code, this is a quick example using the excellent async library.
var async = require('async'),
// Array of apis
httpOptions = [
{
host: 'api.test123.com',
path : '/content/food/?api_key=1231241412',
headers: {
"Accept": "application/json",
'Content-Type': 'application/json'
},
method: "GET",
port: 80
},
host: 'api.test234.com',
path : '/content/food/?api_key=1231241412',
headers: {
"Accept": "application/json",
'Content-Type': 'application/json'
},
method: "GET",
port: 80
}
];
// Put the logic for fetching data in its own function
function getFile(options, done) {
http.get(options, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
done(null, JSON.parse(body));
console.log("Got response: " + jsonFile1);
});
}).on('error', function(e) {
done(e);
console.log("Got error: " + e.message);
});
}
app.get('/', function(req, res) {
// Map the options through the getFile function, resulting in an array of each response
async.map(httpOptions, getFile, function (err, jsonFiles) {
// You should probably check for any errors here
res.render('home', {
data: jsonFiles
});
});
});

Categories