Using request module and passing the body to the next request - node.js

I am using request to get an image:
request(req.body.imageUrl, {encoding: null}, function(error, response, body) {
I then want to use the body to pass to an api that uses a multipart form and send that image (which is now in the body). I don't want to write the file to disk and then readstream from the disk again. I basically want to enter the formData of the next request by using the Buffer of the body, but it is not working.
So, for the options in the next request I have:
const options = {
method: "POST",
url: coreURL,
headers: {
"Content-Type": "multipart/form-data"
},
formData : {
file : new Buffer.from(body,'binary')
}
};
And this does not work, if I write the body to a file fs.writeFileSync(fileName, body, 'binary');
And then read in the options formData : { file : fs.createReadStream(fileName)}
it works but I cannot use the disk, so need an alternative way to pass the body into the next post as multipart form data.
Any ideas?

POC:
let request = require('request');
request.post({
url: "http://httpbin.org/anything",
formData: {
myfile: request.get("https://www.gravatar.com/avatar/f056d36f9e273fd4740ec8a5cea1348a"),
}
},
function (err, req, body) {
console.log(body);
}
);

Related

Node JS request get original url

I'm sending GET requests like this in Node JS in a loop
request({
url : 'https://somelink.com',
method: 'GET'
},
function (error, response, body) {
console.log(response);
});
Since the response is async, is it possible to get the original request URL in the response?
Thanks!
You can get the original request href in the response.request object, like so:
const request = require("request");
request({
url : 'https://google.com',
method: 'GET'
},
function (error, response, body) {
if (!error) {
console.log("Original url:", response.request.uri.href);
console.log("Original uri object:", response.request.uri);
}
});
You can access more information in the request.uri object, for example:
console.log("Original uri:", response.request.uri);
This will give you some more useful information like port, path, host etc.

Send unparsed data in a Node JS request

I want to send a POST request (for example, with the 'request' module), but I don't find a way of sending unparsed data*.
*unparsed data => copied directly from the Chrome dev tool. Something like: tipo_accion=3&filtro=descarga&fecha=actual
It would also do the trick some way of translating that string to JSON.
I've tried so far with...
var request = require('request');
request.post({ url: 'https://target.com/form/', form: 'tipo_accion=3&filtro=descarga&fecha=actual' },
function (error, response, body) {
console.log(body)
}
);
... but it didn't work.
Firstly you should understand the difference between the request methods post and get.
The structure that you want to send:
tipo_accion=3&filtro=descarga&fecha=actual is telling me that you want to use a get request. So the proper code for that will be something like:
request(
'https://target.com/form/&tipo_accion=3&filtro=descarga&fecha=actual',
function (error, response, body) {
console.log(body)
},
);
But if it is a post request then you should use the json format
request.post({
url: 'https://target.com/form/', form: {
tipo_accion: 3,
filtro: 'descarga',
fecha: 'actual'
},
function(error, response, body) {
console.log(body)
}
);
You can convert form_data to string format and it works for me you can try it :
const request = require('request-promise');
var j = request.jar()
var data_fom = `a=value_a&b=value_b&c=value_c`
request({
url:"https://target.com/form/",
jar:j,
method:"POST",
resolveWithFullResponse: true,
form:data_form
})

Unsupported media type when uploading form data to spring server

I am trying to upload a file to a remote spring server via an API, and I keep getting an unsupported media type error (415) even though I have already made the data into form data.
Here is the express http post request:
var FormData = require('form-data');
var fs = require('fs');
var form = new FormData();
form.append('pid', params.pid);
form.append('deliveryAttachment', fs.createReadStream(params.deliveryAttachment.path));
var url = someDomain + '/proj/new/deliveryAttachment';
requestLib({
url: url,
method: "POST",
jar: getJar(),
form: form
},function (error, response, body){
console.log(body)
});
And here is the Java Spring controller for reference:
#RequestMapping(value = "proj/new/deliveryAttachment", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
public String insertDeliveryAttachment(#RequestParam("pid") long pid,
#RequestParam("deliveryAttachment") MultipartFile file) {
try {
DeliveryAttachment a = new DeliveryAttachment(file.getOriginalFilename(), pid);
ps.insertDeliveryAttachment(a, file.getBytes());
return String.valueOf(a.id);
} catch (IOException e) {
return "-1";
}
}
This is the form data console log:
And the 415 response:
{
"timestamp": 1494671395688,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded' not supported",
"path": "/proj/new/deliveryAttachment"
}
--UPDATE--
Alright, I just found out after reading request's docs that if you use form as the holder for the data, it will treat the data as application/x-www-form-urlencoded
e.g.; request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ ... });
Meanwhile the correct key for multipart/form-data is formData
e.g.; request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) { ... });
I tried it, and now it's giving me a new error:
TypeError: Cannot read property 'name' of null at FormData._getContentDisposition
It seems that you are sending a POST request with Content-Type: 'application/x-www-form-urlencoded', and your SpringController insertDeliveryAttachment() consumes multipart/form-data mime-type.
I would recommend you to change the consumes mime-type on your insertDeliveryAttachment() method to MediaType.APPLICATION_FORM_URLENCODED_VALUE
I've solved it. I did not use FormData and just went with inserting the values inside an object and it worked.
var data = {
pid: params.pid,
deliveryAttachment: fs.createReadStream(params.deliveryAttachment[0].path)
};
var url = wfDomain + '/proj/new/deliveryAttachment';
requestLib({
url: url,
method: "POST",
headers: {
'Content-Type': 'multipart/form-data'
},
jar: getJar(),
formData: data
},function (error, response, body){ ... });

calling external rest api from node without encoding querystring params

I am trying to call an external rest API from node server by using request node module.
let request = require('request');
var options = {
method: 'POST',
url: 'https://somerestURI:3000',
qs: { msg: 'some|data|for|other|server' }
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
If I try to run the above code, query string value is being encoded to
some%7cdata%7cfor%7cother%7cserver
as a result I am not receiving correct response.
But if I fire the same request in POSTMAN. I am receiving the expected output(I think postman is not encoding query string).
So what I want is don't encode the query string value.
Any help would be greatly appreciated.
As answered here, you can disable encoding in qsStringifyOptions
var options = {
method: 'POST',
url: 'https://somerestURI:3000',
qs: { msg: 'some|data|for|other|server' },
qsStringifyOptions: {
encoding: false
}
};
You can use node-rest-client package. It allows connecting to any REST API and get results as javascript Object.
var HttpClient = require('node-rest-client').Client;
var httpClient = new HttpClient();
// GET Call
httpClient.get("http://remote.site/rest/xml/method", function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});)
or for POST Call
var args = {
data: { test: "hello" },
headers: { "Content-Type": "application/json" }
};
//POST Call
httpClient.post("http://remote.site/rest/xml/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});

Uploading file using POST request in Node.js

I have problem uploading file using POST request in Node.js. I have to use request module to accomplish that (no external npms). Server needs it to be multipart request with the file field containing file's data. What seems to be easy it's pretty hard to do in Node.js without using any external module.
I've tried using this example but without success:
request.post({
uri: url,
method: 'POST',
multipart: [{
body: '<FILE_DATA>'
}]
}, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
Looks like you're already using request module.
in this case all you need to post multipart/form-data is to use its form feature:
var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', '<FILE_DATA>', {
filename: 'myfile.txt',
contentType: 'text/plain'
});
but if you want to post some existing file from your file system, then you may simply pass it as a readable stream:
form.append('file', fs.createReadStream(filepath));
request will extract all related metadata by itself.
For more information on posting multipart/form-data see node-form-data module, which is internally used by request.
An undocumented feature of the formData field that request implements is the ability to pass options to the form-data module it uses:
request({
url: 'http://example.com',
method: 'POST',
formData: {
'regularField': 'someValue',
'regularFile': someFileStream,
'customBufferFile': {
value: fileBufferData,
options: {
filename: 'myfile.bin'
}
}
}
}, handleResponse);
This is useful if you need to avoid calling requestObj.form() but need to upload a buffer as a file. The form-data module also accepts contentType (the MIME type) and knownLength options.
This change was added in October 2014 (so 2 months after this question was asked), so it should be safe to use now (in 2017+). This equates to version v2.46.0 or above of request.
Leonid Beschastny's answer works but I also had to convert ArrayBuffer to Buffer that is used in the Node's request module. After uploading file to the server I had it in the same format that comes from the HTML5 FileAPI (I'm using Meteor). Full code below - maybe it will be helpful for others.
function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', toBuffer(file.data), {
filename: file.name,
contentType: file.type
});
You can also use the "custom options" support from the request library. This format allows you to create a multi-part form upload, but with a combined entry for both the file and extra form information, like filename or content-type. I have found that some libraries expect to receive file uploads using this format, specifically libraries like multer.
This approach is officially documented in the forms section of the request docs - https://github.com/request/request#forms
//toUpload is the name of the input file: <input type="file" name="toUpload">
let fileToUpload = req.file;
let formData = {
toUpload: {
value: fs.createReadStream(path.join(__dirname, '..', '..','upload', fileToUpload.filename)),
options: {
filename: fileToUpload.originalname,
contentType: fileToUpload.mimeType
}
}
};
let options = {
url: url,
method: 'POST',
formData: formData
}
request(options, function (err, resp, body) {
if (err)
cb(err);
if (!err && resp.statusCode == 200) {
cb(null, body);
}
});
I did it like this:
// Open file as a readable stream
const fileStream = fs.createReadStream('./my-file.ext');
const form = new FormData();
// Pass file stream directly to form
form.append('my file', fileStream, 'my-file.ext');
const remoteReq = request({
method: 'POST',
uri: 'http://host.com/api/upload',
headers: {
'Authorization': 'Bearer ' + req.query.token,
'Content-Type': req.headers['content-type'] || 'multipart/form-data;'
}
})
req.pipe(remoteReq);
remoteReq.pipe(res);

Resources