Sending body fields to server while using angularjs library

31 Views Asked by At

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.

0

There are 0 best solutions below