Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart with JSON #455

Closed
magalhas opened this issue Sep 18, 2014 · 8 comments
Closed

Multipart with JSON #455

magalhas opened this issue Sep 18, 2014 · 8 comments

Comments

@magalhas
Copy link

Hi,

Can you please let me know how to attach a file and send the rest of the data as JSON? I've tried using req.part() but it has been deprecated.

Thanks.

@magalhas
Copy link
Author

No one?

@huang-xiao-jian
Copy link

Actually, I don't understand the description quite well, but code below may be helpful for you.

backend data resolve first.

var express = require('express');
var multer = require('multer');

var app = express();
app.post('/',  multer({
    inMemory: true,
    includeEmptyFields: true
}), function(req, res) {
    /*
     req.files contains file information
     req.body contains other non-file information
     */
    res.json(req.body);
});
app.listen(1337);

superagent with multipart

var request = require('superagent');
var should = require('should');

describe('superagent', function () {
    it('with multi part', function (done) {
        request
            .post('http://localhost:1337')
            .field('name', 'born')
            .field('age', 23)
            .attach('package', 'package.json')
            .attach('index', 'index.js')
            .end(function(res) {
                try {
                    res.body.should.eql({
                        "name": "born",
                        "age": "23"
                    });
                    done();
                } catch (err) {
                    done(err);
                }
            });
    });
});

In theory, supertagent support both part lower-level and field, attach higher-level api . @magalhas

@zbyte64
Copy link

zbyte64 commented Jan 25, 2015

Okay two things,

Is part really deprecated? If so shouldn't it be removed from the docs?

Can attach accept a file stream or a blob of text isntead of reading from a filename?

@formula1
Copy link

formula1 commented May 4, 2015

It appears this was not answered

Heres what I have found if it helps anyone

  • superagent basically is just a wrapper around the form-data module when it comes to multipart post calls. The wrapper transforms strings into readable streams so your best to expect that a normal stream should work as well
  • form-data's append method is a wrapper around combined-stream's append.
  • combined-stream's converts all streams into a delayed stream then pushes the value (regardless of whether its a stream or not) on the list of streams
  • combined-stream's way of handling its list of streams is pretty straight forward. If its a stream, it pipes. If it isn't it just writes the values.

In other words passing a blob or a readableStream seems to be fine.

If you want to pass a content type, you must provide a exstension that will qualify as the type you want. This seems straightforward however it is not in all cases since if you want to send a file as application/x-patch that will not be possible as it is not in the mime-db

Update - I was wrong on the arbitrary readable stream.

@misha-slyusarev
Copy link

I'm using superagent with React and Rails and trying to compose a multipart request for the backend.

Is there a way to add a JS object to the request without adding it field by field? For instance, I would like to have {'invoice' => {'recipient_attributes' => {'name' => 'myname', 'last_name' => 'mylastname'}, 'attachment' => ... } as my params in Rails backend part. And that's how I'm trying to do it:

request.post('v1/invoices')
      .field('invoice[recipient_attributes]', this.state.recipient)
      .attach('invoice[attachment]', this.state.invoiceFile)
      .end(function(err, res){
        console.log('invoiceInfo submitted')
      });

But recipient_attributes is saved as '[object Object]' instead of expected. Is there a way to add the whole object, or I have to add fields one by one like this:

.field('invoice[recipient_attributes][name]', 'myname')
.field('invoice[recipient_attributes][last_name]', 'mylastname')

Thank you in advance!

@kornelski
Copy link
Contributor

You have to add fields one by one.

@ollyde
Copy link

ollyde commented Apr 26, 2021

The fields get converted to strings and fail validation.

@maliksblr92
Copy link

life saver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants