How to post multipart/form-data with node.js superagent

后端 未结 4 1684
独厮守ぢ
独厮守ぢ 2021-02-02 11:09

I am trying to send the content-type in my superagent post request to multipart/form-data.

var myagent = superagent.agent();

myagent
  .post(\'http://localhost/         


        
相关标签:
4条回答
  • 2021-02-02 11:13

    Here is what worked for me. I had a single field form, that was uploading a file. I turned the form into a HTML5 FormData element and then did it as follows:

    var frm = new FormData(document.getElementById('formId'));
    var url =  'url/here';
    
    superagent.post(url)                    
    .attach('fieldInFormName', frm.get('fieldInFormName'))                                        
    .end( function (error, response) {
        //handle response
    });
    

    Please note, I tried various ways of setting the 'Content-Type' manually in superagent, and it never worked correctly because of the multipart identifier needed in the Content-Type.

    0 讨论(0)
  • 2021-02-02 11:16

    First, you do not mention either of the following:

    .set('Content-Type', 'multipart/form-data')
    

    OR

    .type('form')
    

    Second, you do not use the .send, you use .field(name, value).

    Examples

    Let's say you wanted to send a form-data request with the following:

    • two text fields: name and phone
    • one file: photo

    So your request will be something like this:

    superagent
      .post( 'https://example.com/api/foo.bar' )
      .set('Authorization', '...')
      .accept('application/json')
      .field('name', 'My name')
      .field('phone', 'My phone')
      .attach('photo', 'path/to/photo.gif')
    
      .then((result) => {
        // process the result here
      })
      .catch((err) => {
        throw err;
      });
    

    And, let's say you wanted to send JSON as a value of one of your fields, then you'd do this.

    try {
      await superagent
             .post( 'https://example.com/api/dog.crow' )
             .accept('application/json')
             .field('data', JSON.stringify({ name: 'value' }))
    }
    catch ( ex ) {
        // .catch() stuff
    }
    
    // .then() stuff...
    
    0 讨论(0)
  • 2021-02-02 11:25

    Try .type('form') instead of .set('Content-Type', 'multipart/form-data')

    See http://visionmedia.github.io/superagent/#setting-the-content-type

    0 讨论(0)
  • 2021-02-02 11:27

    It is not clear what is in the fields variable that you are sending, but here is some information that may help you determine where your problem lies.

    To begin with, if you are actually trying to build a multi-part request, this is the official documentation for doing so: http://visionmedia.github.com/superagent/#multipart-requests

    as for the error that you got...

    The reason is that during the process of preparing the request, SuperAgent checks the data to be sent to see if it is a string. If it is not, it attempts to serialize the data based on the value of the 'Content-Type', as seen below:

    exports.serialize = {
      'application/x-www-form-urlencoded': qs.stringify,
      'application/json': JSON.stringify
    };
    

    which is used here:

    // body
    if ('HEAD' != method && !req._headerSent) {
      // serialize stuff
      if ('string' != typeof data) {
        var serialize = exports.serialize[req.getHeader('Content-Type')];
        if (serialize) data = serialize(data);
      }
    
      // content-length
      if (data && !req.getHeader('Content-Length')) {
        this.set('Content-Length', Buffer.byteLength(data));
      }
    }
    

    this means that to set a form 'Content-Type' manually you would use

    .set('Content-Type', 'application/x-www-form-urlencoded')

    or

    .type('form') as risyasin mentioned

    any other 'Content-Type' will not be serialized, and Buffer.byteLength(data) will subsequently throw the TypeError: Argument must be a string exception if the value of your fields variable is not a string.

    0 讨论(0)
提交回复
热议问题