api test returns bad request for upload pdf file #27281
Unanswered
darec89
asked this question in
Questions and Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
this is the code.
Please tell what is wrong.
Postman and swagger returns 200.
//
function generateBoundary() {
return
----WebKitFormBoundary${Math.random().toString(16).slice(2)}
;}
function createMultipartBody(fileContent, boundary) {
return
--${boundary}\r\n
+Content-Disposition: form-data; name="user"\r\n\r\n
+${JSON.stringify({ name: "aleks", surname: "markovic", email: "[[email protected]](mailto:[email protected])" })}\r\n
+--${boundary}\r\n
+Content-Disposition: form-data; name="document_type"\r\n\r\n
+authorization\r\n
+--${boundary}\r\n
+Content-Disposition: form-data; name="documents"; filename="5eda1f9076c29.pdf"\r\n
+Content-Type: application/pdf\r\n\r\n
+${fileContent}\r\n
+--${boundary}--
;}
describe('API POST endpoint with image parameter', function () {
it('should upload an image successfully', () => {
cy.fixture('5eda1f9076c29.pdf', 'base64').then((fileContent) => {
const boundary = generateBoundary();
cy.request({
method: "POST",
url: "http://localhost:8000/verify",
headers: {
accept: "application/json",
"Content-Type":
multipart/form-data; boundary=${boundary}
,},
body: createMultipartBody(fileContent, boundary)
}).then((response) => {
expect(response.status).to.equal(200);
});
});
});
});
Beta Was this translation helpful? Give feedback.
All reactions