-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multipart form-data for API Post Request Issue #21173
Comments
Thanks for opening an issue! There is a related discussion here about improvements we wish to make to handle requests with |
Has the work on multipart/form-data been prioritized or completed in the latest version? |
This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided. |
Can confirm that this is still happening as of 12.9.0 |
Still the Issue persist in 12.17.1V as well. Can somebody help and fix this issue ? |
Do we have a work around for this problem? I am using cypress v13.4.0 and I still have the issue. |
++ |
"Hi! If anyone still has a problem with this type of form, I invite you to discuss. I have a solution that has been working for me for quite some time now." |
@BednarOG could you share the solution that has been working for you ? |
In the example of what @jenglishPS wrote.
My example: addSomething() {
cy.fixture("attachFiles/example.png", "binary").then((image) => {
const payload = new FormData();
payload.append("userName", "random user name");
payload.append("email", "random email");
payload.append("file", new Blob([image], { type: "image/png" }), "example.png");
});
cy.request({
method: "POST",
url: endpointURL,
headers: {
"Content-Type": "multipart/form-data",
},
body: payload,
})
} If there is something not entirely clear, let me know. |
@BednarOG I have tried with PDF attachment still getting error I've gotten 400 errors in response , response body getting empty uploadRL(filePath: string): any {
const accessToken = Cypress.env('accessToken');
return cy.fixture(filePath, "binary").then((fileContent) => {
const payload = new FormData();
payload.append("file", new Blob([fileContent], { type: "application/pdf" }), filePath);
return cy.request({
method: 'POST',
url: `${apiconfig.endpoints.postUploadRL}`,
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'multipart/form-data'
},
body: payload,
failOnStatusCode: false
}).as('uploadRL');
});
}
{
"method": "POST",
"url": "xyz",
"headers": {
"Authorization": "Bearer xxxxx
},
"body": {},
"failOnStatusCode": false
} |
Try this: uploadRL(filePath: string): any {
const accessToken = Cypress.env('accessToken');
return cy.fixture(filePath, "binary").then((fileContent) => {
const payload = new FormData();
const blob = new Blob([fileContent], { type: "application/pdf" });
payload.append("file", blob, filePath);
return cy.request({
method: 'POST',
url: `${apiconfig.endpoints.postUploadRL}`,
headers: {
'Authorization': `Bearer ${accessToken}`,
},
body: payload,
encoding: 'binary'
}).as('uploadRL');
});
} |
@BednarOG Still getting 400 error with same response like empty in body i have given the file in this way and converted to a Blob before appending it to the FormData When(`uploadltr`, () => {
const filePath = 'xyz.pdf';
cy.wait(2000);
API.uploadRL(filePath).then((resp: any) => {
response = resp;
cy.log(JSON.stringify(response.body)); // Log response for debugging
expect(response.status).to.eq(200);
});
}); |
First of all, if you only need to upload a .pdf file, you don't need to format it as a Blob. Make sure you have this file in the 'cypress/fixtures/attachFiles' folder, and in the body, provide the path to this file. cy.request({
method: "POST",
url: url.path,
body: {
filePath: "attachFiles/yourFile.pdf",
},
headers: your headers,
}) |
@jennifer-shehane @BednarOG As per the above input tried i have placed my Pdf file in fixture folder still getting 400 , kindly check the below
Here is my request & response :
}Response: 400 150ms |
Try something like this:
|
@BednarOG I have tried this earlier , not working getting response 400 and on request still the body is empty {} |
send me your test file, data file (body) and the API methods that you using. |
@BednarOG I have tried same in Postman tool upload the PDF it is works (simply I'm attaching the PDF file in form data) API i tried this Adding the xyz.pdf in fixture folder and call the UploadRL When(` upload file `, () => {
const filePath = 'xyz.pdf';
cy.wait(2000);
API.uploadRL(filePath).then((resp: any) => {
response = resp;
cy.log(JSON.stringify(response.body)); // Log response for debugging
expect(response.status).to.eq(200);
});
});
uploadRL(filePath: string): any {
const accessToken = Cypress.env('accessToken');
cy.fixture(filePath, 'binary').then((fileContent) => {
// Convert binary content to a Blob object
const blob = Cypress.Blob.binaryStringToBlob(fileContent, 'application/pdf');
// Create a FormData object to include the file
const formData = new FormData();
formData.append('file', blob, filePath);
cy.request({
method: 'POST',
url: `${apiconfig.endpoints.postUploadRL}`,
body: formData,
headers: {
'Authorization': `Bearer ${accessToken}`,
},
// Important: Do not set Content-Type here, let the browser set it for the form-data
failOnStatusCode: false
}).as('uploadRL');
});
return cy.get('@uploadRL');
} |
Current behavior
I've tried many different formats/variations to post an API request which includes a multipart form-data request and a csv file to the body of an API request. The from data versions I've tried do not attach any data to the body, and when trying to us a binary to blob conversion, the request does not work. I've also tried custom commands and those do not submit my request. XMLHttpRequest does not work for me either.
I've gotten 400 errors because the file wasn't attached/uploaded and in different versions, "Failed to read the request form. Unexpected end of Stream, the content may have already been read by another component. "
Desired behavior
I'm expecting to receive a 200 status with a returned id from the api.
Test code to reproduce
To reproduce aws access would be needed and the call contains private data. Here is layout of the code trying to be run:
Cypress Version
9.5.2
Other
No response
The text was updated successfully, but these errors were encountered: