cypress: POST request using formdata doesn't work
I created the following command in cypress commands.js: -
Cypress.Commands.add("form_request", (method, urlPathParam, formData) => {
return cy
.server()
.route(method, "https://admin.teamapp.myhelpling.com" + urlPathParam)
.as("formRequest")
.window()
.then(win => {
const xhr = new XMLHttpRequest();
xhr.open(method, "https://admin.teamapp.myhelpling.com" + urlPathParam);
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("access-token", accesstoken);
xhr.setRequestHeader("client", client);
xhr.setRequestHeader("expiry", expiry);
xhr.setRequestHeader("token-type", tokentype);
xhr.setRequestHeader("uid", uid);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(formData);
})
.wait("@formRequest");
});
Kindly note that I’m reading all header values from previous login request.
In the test spec file I’m doing the following inside a Test case: -
cy.AppLogin();
let dataname = "Test FORMDATA API";
let formData = new FormData();
formData.append("client[name]", dataname);
formData.append(
"client[client_logo_attributes][content]",
cy.fixture("/images/clients/Golden JPEG.jpeg")
);
cy.form_request("POST", "/admin/clients", formData).then(response => {
cy.log(response.status);
});
On cypress electron app I see the following error: -
CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'formRequest'. No request ever occurred.
Commenting .wait("@formRequest"); out from commands.js code above I get the following error: -
POST https://admin.teamapp.myhelpling.com/admin/clients 405 (Method Not Allowed)
In the sources tab on dev tool I see a X(cross) mark on xhr.send(formData) line provided below: -
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryAGOGnFVPveAa8gfv");
xhr.send(formData);
}); //.wait("@formRequest");
});
I’ve no idea what to do here. I’m also new to cypress. Kindly suggest.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 25 (5 by maintainers)
I wish this was supported in Cypress without having to do workarounds. This is a very common use case.
@Vinod-Gulia @lorennorman Appreciate your feedback for above request plz
@gitsaquib The solution for me was to use “fetch”.
@arthsiddh Are you lazy or is it too difficult to test it by yourself?
You can follow this
https://youtu.be/M7ofat3ZI4s
On Wed, 17 Mar, 2021, 2:15 pm SanjayVeernala, @.***> wrote:
@Elshaikh, I think you should be able to update the
this.bodyjsonto just bebodyjsonto match the arguments being received to the.then()callback function. Does this work?