I have a specific task: I need to download an image from the source URL and upload it to another host via POST request as multipart/form-data. I'm trying to use node.js request library but never succeed.
The following code doesn't send anything in the request body.
request.post({
url: uploadUrl,
formData: {
photo: request(imageUri)
}
}, function (err) {
if (err) console.error(err.stack)
});
I have tried posting directly through the form-data library, but it doesn't seem to work neither. How do I solve this without the creation of temp files?
As i said in my comment, you need to wait until you have the image to make the post request. If you wanted to pipe the streams, you could try something like this...
request.get(imageUri).pipe(request.post(uploadUri));
Hope that helps.
The problem turned out to be that my imageUri had query parameters in it. I think this is a bug in form-data library. Removing query parameters solved the problem.
Related
I have written auth routes in Nestjs and wanted to use it with the form-data. I got it working with URL-encoded-form-data, JSON, text, but not receiving anything in the body when I use form-data and really want it to work with form-data as on the front-end I am hitting the route with form-data. I have tried every way I could find on the web, but none of them helped me in this case. so after hours of searching and trying when I didn't get any lead I am posting my question here.
Any Kind of Help is appreciated.
Code of signup endpoint
Main.ts configurations
empty body I am getting on postman
NestJS provides a built-in multipart/form-data parser which you can access using a FileInterceptor.
Here's an example of how you'd read a single file (as you've shown in your screenshot):
#Post('signup')
#UseInterceptors(FileInterceptor('<name of file here - asdasd in your screenshot>'))
signup(#UploadedFile() file, #Body() body) {
console.log(file);
console.log(body);
}
I am trying to send a POST request with query parameters using the request library. I have succeded in sending these requests, but I am still unable to include any working query.
I have followed instructions from other posts and concluded that I’ve tried everything on the internet that is related to my mission.
This is the code that does comply with what I found on the internet:
const request = require(’request’);
request.post({
url: 'https://example.com/endpoint',
qs: { with_counts: true } },
function (err, res, body) {
console.log(err, body);
}
);
This, however, does not work. The server does not recieve the query. I tried setting the body and json properties instead, but those didn’t work either.
I cant tell what I’m doing wrong, and I realize that it might not be possible for you to tell, but this is my last desperate attempt before giving up and switching to Python.
There is an external (I can't change it) API for creating articles. Using this API I succeed create an article this way:
curl -F "article[id]=607822" -F "article[file]=#/article_file_example/article" 'https://api/v2/articles'
How can I perform this following http request using Node.js (preferably with axios)?
The code should work for both Linux and Windows, so I can't use require('child_process').exec
I tried the following:
const formData = {
'id': 607822,
file: fs.createReadStream(filePath), //filePath is the absolute path to the file
};
axios.post('https://api/v2/articles', {article: formData});
But the API response is validation error, which means I didn't succeed to imitate the curl command.
Any suggestions?
P.S. There is also an impovement I would like to perform once I have a working solution. I have the file content, so I prefer to send it directly without creating the file + createReadStream. I tried to do that Blobing the context, but didn't succeed.
Looks like axios doesn't support sending multipart/form-data: https://github.com/mzabriskie/axios/issues/789
My solution was based on one of the answers for:
Nodejs POST request multipart/form-data
I used restler module.
I couldn't send my data just as:
{
article: {
'id': 607822,
file: restler.file(filePath, null, stats.size, null, "text/plain")
}
}
since it wasn't retrieved correctly by server.
So I had to transform it to:
{
'article[id]': 607822,
'article[file]': restler.file(filePath, null, stats.size, null, "text/plain")
}
Okay so what I am trying to achieve here is that I have made an PHP script with a bunch of $_GET's.
So I would like to go to a website without actually visiting it with NodeJS. Basically to trigger this script with parameters.
I hope that explains what I am trying to do. I really have no idea how to make it trigger a site or whatever you can call it - hence why not posting anything that I have tried before posting this.
Thanks in advance.
To make an HTTP request from node, you may use the standard http package. You'll find examples in the standard documentation.
To make them in a simpler way, you may also use the request module. Using it, a http request is as simple as that :
var request = require('request');
request("https://yourwebsite.com/page.php?a=b", function(error, res, body){
if (error) console.log("error:", error);
else console.log('body of the response :', body);
})
I'm looking at using NodeJS to act as the server to build an API.
Ideally I'd love for there to be an API endpoint to send a set of information as well as a file which can be saved to the files system.
Most examples I've seen are for sending a file via a form, however I'd like to do this through a post request.
Does anyone know how I could achieve this (if it's at all possible)?
At the moment what I'd like to achieve is something along the following lines:
app.post('/Some/Endpoint/', controller.handleSomeEndpoint, function(request, response) {
response.send('Finished Request');
});
exports.handleSomeEndpoint = function(request, response, next) {
var bodyarr = []
request.on('data', function(chunk){
bodyarr.push(chunk);
})
request.on('end', function(){
console.log( bodyarr.join('') );
})
}
But the data and end never get called if I run a curl command along the lines of:
curl http://127.0.0.1:5000/Some/Endpoint/ -F 'test=#test_file'
Cheers,
Matt
The answer seems to be that expressJS doesn't use the same method of handling a post file as the http module in nodejs.
All that was needed was including a directory for the files to be written to:
app.use(express.bodyParser({uploadDir:'./uploads'}));
Which I found here:
http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html
I would suggest you to use Formidable to avoid anonymous file uploading.
Your code should work fine; it's the curl usage that's wrong. Try this instead:
$ curl -X POST --data-binary #test_file http://localhost:8080