Nodejs pass formdata foreward - node.js

Front end => Middel service => End service
My front end sends a file to my nodejs service by doing this:
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
resolve(xmlHttp.responseText);
}
}
var data = new FormData();
data.append('file', event.target.files[0]);
xmlHttp.open('POST', '.../utUploadFile');
xmlHttp.send(data);
My End service then catches this and saves the file:
app.post('/utUploadFile', async (request, response, next) => {
const form = new formidable.IncomingForm();
form.parse(request, async function (err, fields, files) {
...save the 'files'
});
});
My question is, how do I add a middle layer to this, how to I just take the front end request, and pass the same thing on to the End service to do its work:
const http = require('request');
...
app.post(`/utUploadFile`, (request, response, next) => {
http.post({
headers: {'content-type': 'multipart/form-data'},
url: `.../utUploadFile`,
body: JSON.stringify(request.body)
}, (err, res, body) => {
});
});

Related

How can I fix my code to make it send the json data as a response to postman's GET requests?

The data I am receiving through the code is outputting to the cmd through console.log but I can't seem to figure out how to make that same data available for GET requests from postman. Thank you
const express = require('express');
const app = express();
const PORT = 5000;
const apicall = require('./apicall');
const request = require('request');
app.get('/', (req, res) => {
res.send("Hello world!")
});
app.get('/getinfo', (req, res, body) => {
const getToken = (url, callback) => {
const options = {
url: process.env.GET_TOKEN,
json: true,
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'client_credentials'
}
};
request.post(options, (err, res, body) => {
if(err) {
return console.log(err)
}
console.log(`Status: ${res.statusCode}`)
console.log(body);
callback(res);
});
}
var AT = '';
var info = '';
getToken(process.env.GET_TOKEN, (res) => {
AT = res.body.access_token;
return AT;
});
const getGames = (url, accessToken, callback) => {
const gameOptions = {
url: process.env.GET_GAMES,
method: 'GET',
headers: {
'Client-ID': process.env.CLIENT_ID,
'Authorization': 'Bearer ' + accessToken
}
};
request.get(gameOptions, (err, res, body) => {
if(err) {
return console.log(err);
}
let x = '';
console.log(`Status: ${res.statusCode}`);
console.log(JSON.parse(body));
//res.send(parsed);
//req.body.getinfo = JSON.parse(body);
})
}
setTimeout(() => {
getGames(process.env.GET_GAMES, AT, (response) => {
});
}, 1000);
//res.send(JSON.parse(body));
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});
You use res.send in the callback of a request.get. But in that context, res is the incoming response from the API that you call, not the outgoing response created by your app. Only the outgoing response contains a send method.
To keep both separate, use different names:
app.get("/getinfo", function(req, res) {
request.get(..., function(err, incoming_res, body) {
res.json(JSON.parse(body));
});
});
res.send is a part of express. If the res.send that's failing is in request.get then that's because it's not a part of express.
From the docs for request it says that the response argument will be an instance of http.IncomingMessage. That should mean you can simply use res.end
Edit:
#HeikoTheißen is right. There is no res.end.
But this could be handled in a different way. If we can wrap the get request inside a promise, then we could resolve the promise with whatever needs to be sent from the get request.
An example:
const result = await new Promise((resolve) => {
request(gameOptions, function (error, response, body) {
resolve ({status : 'A Ok!'}) // <--- send response here
}
}
console.log ("result is ", result) // <-- Object {status : 'A Ok!'}
You just pipe it to the response like so .pipe(res)
const express = require('express');
const app = express();
const PORT = 5000;
const apicall = require('./apicall');
const request = require('request');
app.get('/', (req, res) => {
res.send("Hello world!")
});
app.get('/ne2', (req, res) => {
//res.send('This is the new endpoint');
apicall.getCall;
});
app.get('/getinfo', (req, res, body) => {
const getToken = (url, callback) => {
const options = {
url: process.env.GET_TOKEN,
json: true,
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'client_credentials'
}
};
request.post(options, (err, res, body) => {
if(err) {
return console.log(err)
}
console.log(`Status: ${res.statusCode}`)
console.log(body);
callback(res);
});
}
var AT = '';
var info = '';
getToken(process.env.GET_TOKEN, (res) => {
AT = res.body.access_token;
return AT;
});
const getGames = (url, accessToken, callback) => {
const gameOptions = {
url: process.env.GET_GAMES,
method: 'GET',
headers: {
'Client-ID': process.env.CLIENT_ID,
'Authorization': 'Bearer ' + accessToken
}
};
request.get(gameOptions, (err, res, body) => {
if(err) {
return console.log(err);
}
let x = '';
console.log(`Status: ${res.statusCode}`);
//console.log(JSON.parse(body));
info = JSON.parse(body);
console.log(info);
//res.send(parsed);
//req.body.getinfo = JSON.parse(body);
}).pipe(res);
}
setTimeout(() => {
getGames(process.env.GET_GAMES, AT, (response) => {
});
}, 1000);
//res.send(info);
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});

How to pass data between routes in express.js

hope someone can help me. Can't find a solution. Maybe I'm also just on the wrong way?
It's a simple express setup and I'm quite new.
I get a response from a request and want to pass a variable/the data from the response to the next route into the URL.
So one parameter in the next URL should be dynamical depending on the response of the first call.
here my whole code:
My problem is where you can see the const sendoutID
const express = require("express");
const app = express();
const request = require("request");
const bodyParser = require("body-parser");
const port = 3001;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Create Sendout
app.post("/createSendout", (req, res, next) => {
request.post(
{
url: "https://www.something.com/api/v1.2/surveys/904211/sendouts",
body: JSON.stringify(req.body),
headers: {
"Content-Type": "application/json",
"X-API-KEY": "xxxx-xxx-xxxx-xxx-xxxxxxx",
},
},
function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results.CreateSendoutResult.SendoutId); // View Results
// I want this data "results.CreateSendoutResult.SendoutId" passing to the next route
}
}
);
});
/* here the variable is just hard coded for now but
I want to pass it in the URL from my previous route
to the next route see below at + sendoutID +..*/
const sendoutId = 389125;
// Add Respondent
app.post("/addRespondent", (req, res, next) => {
request.post(
{
url:
"https://www.something.com/api/v1.2/surveys/904211/sendouts/" +
sendoutId +
"/respondents",
body: JSON.stringify(req.body),
headers: {
"Content-Type": "application/json",
"X-API-KEY": "xxxxxx-xxx-xxx-xxx-xxxxxxxx",
},
},
function (error, response, body) {
console.log(response);
//console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results); // View Results
}
}
);
});
app.listen(port, () => {
console.log(`app listening at http://localhost:${port}`);
});
To pass to the next route you can assign results.CreateSendoutResult.SendoutId to req.body
req.body.SendoutId = results.CreateSendoutResult.SendoutId;
Then you can use that SendoutId in next route.
You can pass that variable inside next()
next(results.CreateSendoutResult.SendoutId);
In the next route you can access it by calling:
function nextRoute(SenderId, req, res, next)
Edited:
const express = require("express");
const app = express();
const request = require("request");
const bodyParser = require("body-parser");
const port = 3001;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Create Sendout
app.post("/createSendout", (req, res, next) => {
request.post(
{
url: "https://www.something.com/api/v1.2/surveys/904211/sendouts",
body: JSON.stringify(req.body),
headers: {
"Content-Type": "application/json",
"X-API-KEY": "xxxx-xxx-xxxx-xxx-xxxxxxx",
},
},
function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results.CreateSendoutResult.SendoutId); // View Results
// I want this data "results.CreateSendoutResult.SendoutId" passing to the next route
req.SendoutId = results.CreateSendoutResult.SendoutId;
}
}
);
}, addRespondent);
/* here the variable is just hard coded for now but
I want to pass it in the URL from my previous route
to the next route see below at + sendoutID +..*/
const sendoutId = 389125;
// Add Respondent
app.post("/addRespondent", addRespondent);
function addRespondent(req, res, next) => {
request.post(
{
url:
"https://www.something.com/api/v1.2/surveys/904211/sendouts/" +
req.sendoutId +
"/respondents",
body: JSON.stringify(req.body),
headers: {
"Content-Type": "application/json",
"X-API-KEY": "xxxxxx-xxx-xxx-xxx-xxxxxxxx",
},
},
function (error, response, body) {
console.log(response);
//console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results); // View Results
}
}
);
}
app.listen(port, () => {
console.log(`app listening at http://localhost:${port}`);
});
You should be using a middleware function.
What is a middleware function?
-> it's just a function that runs in the middle that is (before a request hits the route and ends before a request completes.Express Documentation for writing middlewares.
Middleware functions are functions that have access to the request object (req), the response object (res), and the next function.NOTE middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging..
Advantage of using a middleware function -> "you can use this function for any other request too in the future", use it in other modules".
2)
const captureSendOutIDMiddleware = async (req, res, next) => {
try {
req.SendOutID=results.CreateSendoutResult.SendoutId; //changed "const req" to just req.
next();
} catch (error) {
res.status(401).send({
error: 'NO SEND OUT ID'
})
}
app.post("/CreateSendOut",captureSendOutIDMIddleware,async(req, res) =>{
//do your operation
});
app.post("/addRespondent",async(req,res)=>{
const capturedSendoutID=req.SendoutID;
console.log(capturedSendoutID);
//do you operations
});
Thanks for your help.
I found now another solution which works as well as I found another issue. Don't know if it was also a cause for why it didn't work, or why your solutions didn't work. But I used app.set() and app.get to pass the data.
The other issue was, that now, with app.set() and app.get() it sometimes worked, sometimes not. So I set a timeout on the call in the frontend, which executes the api requests. Just to have a bit time between.
here is my new code
// Create Sendout
app.post("/createSendout", (req, res, next) => {
request.post(
{
url: "https://www.something.com/api/v1.2/surveys/904211/sendouts",
body: JSON.stringify(req.body),
headers: {
"Content-Type": "application/json",
"X-API-KEY": "xxxxxx-xxx-xx-xxxx-xxxxx",
},
},
function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results.CreateSendoutResult.SendoutId); // View Results
app.set("surveyId", results.CreateSendoutResult.SendoutId); // new added line
}
}
);
});
// Add Respondent
app.post("/addRespondent", (req, res, next) => {
const surveyId = app.get("surveyId"); // new added line
request.post(
{
url:
"https://www.something.com/api/v1.2/surveys/904211/sendouts/" +
surveyId +
"/respondents",
body: JSON.stringify(req.body),
headers: {
"Content-Type": "application/json",
"X-API-KEY": "xxxxxx-xxx-xxxx-xxxx-xxxxxxxx",
},
},
function (error, response, body) {
console.log(response);
//console.log(response.statusCode);
if (!error && response.statusCode == 200) {
// Successful call
var results = JSON.parse(body);
console.log(results); // View Results
}
}
);
});

how to get data outside request

I have a scenario where i need to take response (body) of request method outside request. How can i do it?
request.post({
url: 'http://localhost/api/messages',
form: { key: message }
}, function (err, httpResponse, body) {
tmsg = body;
})
console.log(tmsg);
I need this tmsg outside for next processing, Actual scenario is as below.
app.post('/incomemsg', function (req, res) {
var mediaCount = req.body.NumMedia;
if (mediaCount == 0) {
//var twiml = getResponse(message);
var twiml = new MessagingResponse();
request.post({
url: 'http://localhost:3978/api/messages',
form: { key: message }
}, function (err, httpResponse, body) {
tmsg = body;
})
console.log(tmsg);
}else {
//dosomething which outputs tmsg
}
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(tmsg.toString());
});
The problem is you are trying to assign value to a global variable in request.post's callback() which is only called after request.post is executed by Asynchronous logic(API calls are all async), so a better way would be to promisify request.post and await the request.post to make it seem synchronous.
const requestPromisified = requestObject =>
new Promise((resolve, reject) => {
request.post(requestObject, function(err, httpResponse, body) {
if (err) {
reject(err);
}
resolve(body);
});
});
const body = await requestPromisified({
method: "POST",
url: "http://localhost/api/messages",
form: { key: message }
});
You only can do something with tmsg when you made the request so you need to rearrange your code like this:
app.post('/incomemsg', function (req, res) {
var mediaCount = req.body.NumMedia;
var twiml = new MessagingResponse();
request.post({
url: 'http://localhost:3978/api/messages',
form: { key: message }
}, function (err, httpResponse, body) {
tmsg = body;
console.log(tmsg);
if (mediaCount === 0) {
//do something with tmsg
} else {
//do something else with tmsg
}
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(tmsg.toString());
});
});
Otherwise tmsg will be null because there was no request made to fill that variable.

Authorization issue when downloading AMZ Advertising Report

I have the following app to download productAds from amazons Sponsored Products API
Docs: https://advertising.amazon.com/API/docs/reference/reports
When I try to fun it, I get the response from requesting the report, and obtain a file in an s3 Bucket.
server is running at localhost:3000
{"reportId":"amzn1.clicPI.v1.p7.5B7EE.d141f-e5-4b-a8-6a4e712","status":"SUCCESS","statusDetails":"Report has been successfully generated","location":"https://advertising-api.amazon.com/v1/reports/amzn1.clsAPI.v1.p7.5B7EE.d151f-e5-4b-a8-699e712/download","fileSize":22}
Next I try to download the report
and the repose I get the following erro:
donwloading <?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidArgument</Code><Message>Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified</Message><ArgumentName>Authorization</ArgumentName><ArgumentValue>bearer Atza|IwEBIJvCt20TUi122srkN4JCOdUxlBNuLJBrtbIGF9x5QbKG67f-K-0L4RkLzeyouXWy_U_-VscaCe1aFqOJK55X9Mu2X6nwWkAWRyhc6cCMfPjKpyyVjKtPqC8Plme84om1dqtmIqC93yUVc_clHimQqmnl262te2EXyUhYoVQg8hK2nlDG67Iw7xjsLK4rgl3E4RR36DHnZkEOnVQZtfjIkIbcYtsCSAdpRZRazF4FQfpS-jHvMlwuH8TZfY9tRpmBEx5fjJw1WZ14Dejqti23mZ7yt-MjNkUuD-DdPXs3fek1ZJePlHEVzcI2y_WzCnwJnoSVp5a5w1WgNco8YqEGEuLsT9S0dxQRluTiw8f4b4lx2FFFm9jz0K7pqo1Mvs6DZOVCDJzE-xJ_VLlWoE5QDMUAMor4AEQH44_0NWBjJDrYaVvn1vZLCER1uxW4jgr127W5yXaj4y1vj_vADwFq9a3330hAc73EWwL6FFSfoTQZyNc4Fh1d3DXfVHpXFk6cv0bHt4cV_OotGwGHat5fv75VHX5K3al3Xd5-QJv2cTiQ9srY5oqKsdbxptGaxAdrdMQaUlFHhyHEGbwED9xYoCw6-IauN15gvMAei9wz2kzRCA</ArgumentValue><RequestId>BDCE812C4363E9D2</RequestId><HostId>4ixH24mPtMBt+FDnI3rM9UJP95toaNBmmR1v0uQJ5XkyiXbtLEuZ8d+vDI0+gquwhn6/Fkz6/+o=</HostId></Error>
Here is the app. How can I download the report and pass the headers correctly? I found this repo in PHP that has the download function: https://github.com/dbrent-amazon/amazon-advertising-api-php/blob/master/AmazonAdvertisingApi/Client.php
const express = require('express')
const app = express();
const request = require('request');
const moment = require('moment');
const fs = require('fs')
const path = require('path')
const config = require('./config')
const auth = require('./helpers/auth');
const cron = require('./helpers/cron');
const con = require('./helpers/database');
const getProfiles = (headers) => {
return new Promise((resolve, reject) => {
request({
url: config.ad_url + '/v1/profiles',
method: "GET",
headers: headers
}, (err, httpResponse, body) => {
if (err) {
reject(err);
}
resolve(body);
});
})
}
const createReport = (params, recordType, headers) => {
return new Promise((resolve, reject) => {
request({
url: config.ad_url + '/v1/' + recordType + '/report',
method: "POST",
headers: headers,
json: params
}, (err, httpResponse, body) => {
if (err) {
reject(err);
}
resolve(body);
});
})
}
const getReport = (reportId, headers) => {
return new Promise((resolve, reject) => {
request({
url: config.ad_url + '/v1/reports/' + reportId,
method: "GET",
headers: headers
}, (err, httpResponse, body) => {
if (err) {
reject(err);
}
resolve(body);
});
})
}
function download(fileUrl, apiPath, callback) {
var url = require('url'),
http = require('http'),
p = url.parse(fileUrl),
timeout = 10000;
var file = fs.createWriteStream(apiPath);
var timeout_wrapper = function (req) {
return function () {
console.log('abort');
req.abort();
callback("File transfer timeout!");
};
};
console.log('before');
var request = http.get(fileUrl).on('response', function (res) {
console.log('in cb');
var len = parseInt(res.headers['content-length'], 10);
var downloaded = 0;
res.on('data', function (chunk) {
file.write(chunk);
downloaded += chunk.length;
process.stdout.write("Downloading " + (100.0 * downloaded / len).toFixed(2) + "% " + downloaded + " bytes" + isWin ? "\033[0G" : "\r");
// reset timeout
clearTimeout(timeoutId);
timeoutId = setTimeout(fn, timeout);
}).on('end', function () {
// clear timeout
clearTimeout(timeoutId);
file.end();
console.log(file_name + ' downloaded to: ' + apiPath);
callback(null);
}).on('error', function (err) {
// clear timeout
clearTimeout(timeoutId);
callback(err.message);
});
});
// generate timeout handler
var fn = timeout_wrapper(request);
// set initial timeout
var timeoutId = setTimeout(fn, timeout);
}
const recordType = 'productAds';
const campaignType = 'sponsoredProducts';
const reportDate = moment().format('YYYYMMDD');
const metrics = 'campaignId,sku,currency,attributedConversions1d';
const reportParam = {
campaignType: campaignType,
// segment: "query",
reportDate: reportDate,
metrics: metrics
}
auth().then(res => {
const headers = {
'Content-Type': 'application/json',
'Authorization': config.token_type + ' ' + res.access_token,
'Amazon-Advertising-API-Scope': '196354651614',
}
// getProfiles(headers).then(res => {})
const reportId = 'amzn1.clicksAI.1.p.5B057EE.d41f-e5-4a-a8-699e712';
getReport(reportId, headers).then(res => {
console.log(res)
const file = JSON.parse(res);
// console.log(file.location )
// download
request({
url: file.location,
method: 'GET',
headers: {
'Authorization': headers.Authorization,
'Amazon-Advertising-API-Scope': '196335474',
'Allow': 'GET, HEAD, PUT, DELETE'
}
}, (err, response, body) => {
console.log('donwloading', body)
})
})
createReport(reportParam, recordType, headers).then(res => {
const reportId = 'amzn1.clicksI.v1.p7.5B07EE.d1541f-e5-eb-a68-6996e712';
})
})
app.listen(3000, function(err) {
console.log("server is running at localhost:3000");
})
This is caused by sending the "Authorization" header to S3 and it doesn't want that. The reason is because the /download response from the API is a 307 redirect so the client you are using is redirecting with full API headers. To get around this, prevent your client from following redirects and parse the S3 location from the header of the response instead.

Post request to external api

after a post request from an ajax call in angularjs, i want to send the request params from angularjs to an external api. I get all params i want. But I don't know, how i can make a new post request to the api, inside my nodejs url. I need this step to nodejs.
This is my Code
router.post({
url: '/user/:id/sw'
}, (req, res, next) => {
var userId = req.pramas.id;
var firstName = req.pramas.firstName;
var lastName = req.pramas.lastName;
var data = 'test';
res.send(200, data);
});
I found some solutions like this on: (just example code)
request({
uri: 'http://www.giantbomb.com/api/search',
qs: {
api_key: '123456',
query: 'World of Warcraft: Legion'
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
res.json(body);
} else {
res.json(error);
}
}
});
but this doesn't work. How I can make a new Post Request with the req.params to an external api? Also i need a Response from the api..
Thanks for help and Ideas :)
Its req.params not req.pramas
Try this
var request = require('request');
router.post({
url: '/user/:userId/shopware'
}, (req, res, next) => {
var params = req.params;
request.get({
uri: 'http://www.giantbomb.com/api/search',
qs: params // Send data which is require
}, function (error, response, body) {
console.log(body);
});
});
Try this,
const request = require('request-promise')
const options = {
method: 'POST',
uri: 'http://localhost.com/test-url',
body: {
foo: 'bar'
},
json: true
// JSON stringifies the body automatically
};
​
request(options)
.then(function (response) {
// Handle the response
})
.catch(function (err) {
// Deal with the error
})
var request = require("request");
exports.checkstatus = async (req, res) => { //this is function line you can remove it
try {
var options = {
method: 'POST',
url: 'https://mydoamin/api/order/status',
headers:
{
signature: '3WHwQeBHlzOZiEpK4yN8CD',
'Content-Type': 'application/json'
},
body:
{
NAME: 'Vedant',
ORDERID: 'ORDER_ID1596134490073',
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body); //get your response here
});
} catch (error) {
return fail(res, error.message);
}
};

Resources