cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.08k stars 3.19k forks source link

cypress request with FormData not parsed to json #24560

Open yelhouti opened 2 years ago

yelhouti commented 2 years ago

Current behavior

When sending a request with FormData even if the server retrun application/json cypress keeps the response as ArrayBuffer

Desired behavior

It should parse it and have body as json

Test code to reproduce

const product = {};
formData.set('data', new Blob([JSON.stringify(product)], {type: 'application/json'}));
cy.request({
  method: 'POST',
  url: '/api/products',
  body: formData,
}).then(({ body }) => {
  console.log(body)
})

Cypress Version

10.8.0

Node version

16.13.0

Operating System

Fedora 36

Debug Logs

No response

Other

Workaround

cy.request({
    data
  }).then(res => {
    if (data.body instanceof FormData) {
      res.body = JSON.parse(new TextDecoder().decode(res.body));
      return res;
    } else {
      return res;
    }
  }).then(({ body }) => {
  console.log(body)
})
marktnoonan commented 2 years ago

I was able to make a reproduction of this and is does seem to me that we shouldn't behave differently based on the contents of the outgoing request, for the same server response: https://github.com/marktnoonan/formdata-test

yelhouti commented 2 years ago

@marktnoonan I totally agree, the solution should be to handle response based on response header content-type no matter the request type.

demeralde commented 1 year ago

I've been encountering this for all multipart/form-data tests I've run. This is for two APIs for separate projects, using separate tech on the back-end (Django and Node).

Not only that, this never happens when I've used Postman to test these endpoints. So it must exclusively be a Cypress problem.

Unfortunately I can't write complete tests because of this issue, so hopefully it gets fixed (it's been a long time since this was reported).

effervescentia commented 1 year ago

I'm facing this too but using the TextDecoder work around for now, any plans for a fix in the near future?

true2fik commented 3 months ago

This issue persists as of 13.13.3, so I am resorting to the TextDecoder workaround as well. I'm interested in knowing if this is on the radar.

Dale-777 commented 1 day ago

also having to use the TextDecoder workaround - Cypress 13.15.0