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

后端 未结 4 1686
独厮守ぢ
独厮守ぢ 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.

提交回复
热议问题