I am attempting to send a PDF file as a POST request.
The API supports the use of @RequestPart
and @RequestParam
:
@RequestPart("file") MultipartFile file;
@RequestParam(value = "document-types", required = false) Set<String> documentTypes;
My approach was as follows:
it('test', () => {
cy.fixture(pdfFilePath, "binary").then(file => {
const data = new FormData();
data.set('file', file);
data.set('document-types', 'New Type');
cy.request({
method: "POST",
url: '/api/v4/documents',
headers: {
accepts: "multipart/form-data",
authorization: authString
},
body: data
}).then((response) => {
expect(response.status).to.eq(200)
});
});
});
However, when I execute this code, I receive:
Status: 400 - Bad Request
Upon inspecting the request in Cypress, I noticed that the body is empty:
Body: {}
https://i.sstatic.net/LjZC3.png
While debugging the code, I observed that the data
being sent is empty, as shown in the attached screenshot:
https://i.sstatic.net/MbTgl.png
I am puzzled as I have used the following steps to set the data
on lines 154 and 155 in the screenshot above:
data.set('file', file);
data.set('document-types', 'New Type');
What am I overlooking here?
Cypress version: 9.0.0