Hello I want to ask a question about using form data to send a file and also update data from user at the same time in ExpressJS. I want to ask if this could be handle all in one using form data (save image and update a profile information) through axios or should it be handle in two (two HTTP Requests)? For example:
formData.append('file', {
uri: file.uri,
type: file.type,
name: file.name,
});
// and append for example a updated info
formData.append('profileUpdate', {
name: newName,
email: newEmail,
age: newAge,
});
then receive it on the express route side and separate each other or can you even do it with form-data request? or is there a way I can send the body apart on a PUT or POST Request (form-data)
Thank you in advance!
Related
I'm looking for away to send multiple images in one GET request from an Expressjs server through an api.
I want to create an image gallery of each users uploaded images in a MEAN stack. When images are uploaded using multer, the image information is saved to mongodb, including the userid of whoever uploaded it.
When on angularjs, I want user to have access to any of the images they have previously uploaded. Currently I'm sending one file on a GET request based on user id. Is there anyway of sending multiple files in one json. I'm currently using Expressjs's res.sendFile, but haven't found any info about sending multiple back yet.
https://expressjs.com/en/api.html#res.sendFile
Here is my current get request:
exports.getUpload = function(req, res) {
Upload.find({createdby: req.params.Id}).exec(function(err, upload) {
errorhandle.errorconsole(err, 'file found');
console.log(upload[0]);
var options = {
root: '/usr/src/app/server/public/uploads/images'
};
var name = "" + upload[0].storedname +"";
console.log(name);
res.sendFile(name, options,function(err) {
errorhandle.errorconsole(err, 'file sent');
});
});
};
You can't with res.sendFile. In fact I don't think you can at all. Maybe with HTTP/2 Server Push
, but I'm not sure.
What you can do is send a JSON response with a link to all the images:
exports.getUpload = async (req, res) => {
const uploads = await Upload.find({ createdby: req.params.Id }).exec()
const response = uploads.map(image => {name: `https://example.com/uploads/images/${image.storedname}`})
res.json(response)
}
Note error handling omitted.
My node app is supposed to POST to an external server, so I'm playing with request from NPM. I want to verify it's working, but I'm not entirely sure I'm doing that right.
I've tried both of these methods
request({
url: url,
method: 'POST',
form: { a: 1}
}
request({
url: url,
method: 'POST',
json: true,
body: { a: 1}
}
In my test when I hit my own server, req.body shows the right object when I do json true. However that just means I'm passing a JSON header. The API I actually need to hit is expecting a normal POST, not JSON.
So when I try to verify that request is working right when I use form, my server says req.body is an empty object.
EDIT
I am posting to external API fine using form, but on my own server, express is leaving request.body as empty object.
See if this works for you:
request.post('http://service.com/upload').form({key:'value'})
How do I make an http request to a webservice on document load using cyclejs?
The examples cover reacting to user input and don't meet my needs.
You may try to create a request stream and pass it to the HTTPDriver.
For example:
const request$ = Rx.Observable.just({
url: 'http://www.google.com',
method: 'GET'
});
Then:
return {
HTTP: request$
};
I am trying out the Slack's API using the incoming webhook feature, posting messages works flawlessly, but it doesn't seem to allow any file attachments.
Looking through I understand I have to use a completely different OAuth based API, but creating more tokens just for the purpose of uploading a file seems odd when posting messages works well, is there no way to upload files to slack with the incoming webook?
No, its is not possible to upload files through an incoming Webhook. But you can attach image URLs to your attachments with the image_url tag.
To upload files you need to use the Slack Web API and the files.upload method. Yes, it requires a different authentication, but its not that complicated if you just use a test token for all API calls.
You can see in the Slack API document that it's easy to add an attachment to the POST message to your webhook. Here is a simple example of sending a text message with an attachment in NodeJS:
import fetch from "node-fetch";
const webhook_url = "https://hooks.slack.com/services/xxxx/xxxx/xxxxxxxx"
const url = "https://1.bp.blogspot.com/-ld1w-xCN0nA/UDB2HIY55WI/AAAAAAAAPdA/ho23L6J3TBA/s1600/Cute+Kitten+13.jpg"
await fetch(webhook_url, {
method: "POST",
body: JSON.stringify({
type: "mrkdwn",
text: "Example text",
attachments: [
{
title_link: url,
text: "Your document: <file name>"
},
],
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
I am trying to submit a form to the server with the params in JSON.
form.submit({
url:'JSONSaveEntry',
method:'POST'
});
but it sends everything as form-www-urlencoded.
I already checked that no field has isFile set to true (but then, it would send as multipart-formdata) and that standardSubmit is false.
I also tried to use
Ext.Ajax.request({
url:'JSONSaveEntry',
method:'POST',
params:form.getValues()
});
and
Ext.Ajax.request({
url:'JSONSaveEntry',
method:'POST',
params:Ext.encode(form.getValues())
});
Every submission is done as form-www-urlencoded, although the docs clearly state "Performs a Ajax-based submission of form values (if standardSubmit is false)". But then, this sentence is already proven wrong because whenever a file field is in the form, the form is submitted as multipart.
So, does anyone know how I can get the form submitted as JSON?
Possibility 2: I know that it works if I submit a model via model.save(), but how would I create a model from a form on-the-fly (without hardcoding the fields twice)?
I think below would solve your purpose.
Ext.Ajax.request({
url:'JSONSaveEntry',
method:'POST',
headers: { 'Content-Type': 'application/json' },
jsonData : JSON.stringify(form.getValues()),
success : function(response){ console.log("response from server")},
failure : function(error){console.log(error)}
});