0

My backend has been modified and now requires a field to be sent along with a file. Before it was enough to just upload the file.

My frontend is using since before this angularjs library:

https://github.com/nervgh/angular-file-upload/wiki/Module-API

I made some research and what I understood you should initiate the library itself more or less this way:

var uploader = new FileUploader({
     url: 'mybackend',
     headers: { 'Content-Type': undefined }
});

And then to add fields you should use "formdata". So I tried doing this:

uploader.onBeforeUploadItem = function (item) {
    item.formData['documentType'] = 'mydoctype';
};

However, on server side I see that the field has been sent as an header field. Not as a body-field. Given how the backend has been code (and already workds with autotests) I really need to mimic the autotest and in this case make the frontend library to call backend by sending body field along with the file itself.

I could not find any docs about this. Any help would be really appriciated.

****EDITED*****

tests are using the npm library called supertest. The request looks like this:

const request = supertest('mybackend')
    .post(`/myendpoint`)
    .field('documentType', "mytype")
    .attach('file', fileBuffer, {filename: 'testfile1.pdf', contentType: 'application/pdf'})
    .expect('Content-Type', /json/)
    .end(handleResponse({}, done));

As you can see, a file is attached on a POST call. Where you also added a filed (a POST parameter). If this is not enough I can try to figure out the full request sent to server upon the supertest-call above.

3
  • It's a bit strange that a file would be uploaded with another body field, can you provide an example of how the json\http request should look? Commented Oct 22, 2019 at 15:18
  • Angular-file-upload is unmaintained. Consider using ng-file-upload Commented Oct 22, 2019 at 19:14
  • I updated the answer with details about the request, please have a look. Commented Oct 23, 2019 at 7:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.