I have my express set up as a kind of proxy, whenever i hit /speise I want express to make a POST request to a certain website with form-data in its body. This is the request I want to perform in express:
I tried using axios and https.request to perform this but I know with neither of them how to parse form-data in the request body. This is the approach I tried using with axios:
axios({
method: 'post',
url: 'https://www.viennafood.at/speise',
headers: {
'Cookie': 'PHPSESSID=u1bdc2pgjuft7biqrbd9ea43c5',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
'etlap_post': 'etlap_post',
'week_31': '31. Woche',
'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
},
data: {
'etlap_post': 'etlap_post',
'week_31': '31. Woche',
'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
}
}).then(function (response) {
console.log(new Date(), 'Callback from request to viennafood');
body = response.data;
res.set('Content-Type', 'text/html')
res.send(body);
console.log(new Date(), 'Sent response to client');
next();
}).catch(function (error) {
console.log(error);
});
And this is the approach with https.request:
var options = {
hostname: HTTPS_HOST,
port: HTTPS_POST,
path: API_ENDPOINT,
method: HTTP_METHOD,
headers: {
'Cookie': 'PHPSESSID=u1bdc2pgjuft7biqrbd9ea43c5',
'etlap_post': 'etlap_post',
'week_31': '31. Woche',
'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
}
}, body = '';
console.log(new Date(), 'Sending request to viennafood');
var requ = https.request(options, function(https_res) {
console.log(new Date(), 'Callback from request to viennafood');
/*
* For future use, if the cookie gets invalid
*/
if (https_res.headers['set-cookie']) {
let sessionId = https_res.headers['set-cookie'].toString().split('=')[1].split(';')[0];
}
https_res.on('data', function(d) {
body += d;
});
https_res.on('end', function() {
res.send(body);
console.log(new Date(), 'Sent response to client');
next();
});
});
requ.end();
Neither of them perform the same request as postman, I hope someone of you can help me!
Try to serialize the request payload using querystring.stringify as suggested in the documentation :
const querystring = require('querystring');
axios({
method: 'post',
url: 'https://www.viennafood.at/speise',
headers: {
'Cookie': 'PHPSESSID=u1bdc2pgjuft7biqrbd9ea43c5',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
'etlap_post': 'etlap_post',
'week_31': '31. Woche',
'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
},
data: querystring.stringify({
'etlap_post': 'etlap_post',
'week_31': '31. Woche',
'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
})
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.