How to send files with superagent

前端 未结 4 850
南笙
南笙 2021-01-03 03:46

So about a month ago I asked a question regarding superagent and sending files, but got no response at all. I would still like to find out how to do this as I enjoy using su

4条回答
  •  有刺的猬
    2021-01-03 04:47

    No one asked, but I think this might benefit a few.

    Using async/await

    describe('My App - Upload Module', function(){
      it('Upload Module - ERROR - upload an expired file', async function(){
        try{
          let res = await _upload_license_file("/tmp/test_license.xml");
        }catch(err){
          err.should.have.status(422);
        }
      });
    });
    
    async function _upload_license_file(fileLocation){
      return superAgent.post(base_url + "/upload")
              .set('Authorization', 'Bearer '+API_TOKEN)
              .set('Content-Type', 'multipart/form-data')
              .attach('xml_file', fileLocation)
    }
    

    I have worked here the error module, you can process the response object the similar way for pass cases.

提交回复
热议问题