filiphric / cypress-plugin-api

Cypress plugin to show your API information in the Cypress UI runner
ISC License
264 stars 35 forks source link

Use cy.api() with blob file returning TypeError #102

Open CarolinaSL opened 1 year ago

CarolinaSL commented 1 year ago

Hello! I'm with problems when I try to send a blob file through formData. The error returned is the following:

image

Some points :

-The same error is obtained using requestMode.

Cypress version: 12.1.0 Cypress-api-plugin version: 2.9.0

image

Here is the piece of code:

 .then((binary) => Cypress.Blob.binaryStringToBlob(binary))
        .then((blob) => {
            var path = pathFileFixture;
            let formData = new FormData();
            formData.append('file', blob, fileName);
            cy.api({
                method: 'POST',
                url: Cypress.env('api_url').file_storage,
                body: formData
filiphric commented 1 year ago

will create support for this soon 👍 thanks for the report. one question - before .then() command, do you use cy.fixture() to load the file?

CarolinaSL commented 1 year ago

Yes, I use cy.fixture() to load the file. I appreciate your effort, thanks!

piotrtar commented 1 year ago

Does it matter if you import a file by import fileName from '../../fixtures/fileName.json'; or cy.fixture()?

filiphric commented 1 year ago

it seems that the formData on its own is not the core of the problem. I have been able to work OK with the following test:

it('uploads a file', () => {

  cy.fixture('logo.png', 'binary').then(image => {
    const blob = Cypress.Blob.binaryStringToBlob(image, 'image/png');
    const formData = new FormData();
    formData.append('image', blob, 'logo.png');

    cy.api({
      method: 'POST',
      url: '/',
      body: formData,
      headers: {
        'content-type': 'multipart/form-data'
      },
    })
  })

});

it doesn’t do too much, but it does not throw the error. is it possible for you to provide an example of what the response for such API call is?

samirf-stubben-edge commented 1 year ago

I'm facing the same issue and it seems to be related to the API response processing. Not the request.

Here is the response from my API:

413 Request Entity Too Large

413 Request Entity Too Large


nginx

Captura de tela 2023-06-14 095934

Captura de tela 2023-06-14 095752

SivAlka commented 1 year ago

Seeing the same issue. I am using a fixture and modifying some objects in the json before passing to the endpoint. The operation itself seems successful as I can see the desired values changed in my application but I get the above mentioned error at the end. There is no error when using cypress native method

cy.fixture('').then((data) => {
    data.key1 = some value;
    cy.putWithTokenAPI(urlPath, accessToken, data).then((response) => {
      expect(response.status).to.eq(200);
    });
  });