0

What: Testing the upload of a file (image) via API using Playwright

In the API Docs, for the request it states:

REQUEST BODY SCHEMA:

multipart/form-data

file (string )

A file to upload. The file should follow the specifications of RFC 2388 (which defines file transfers for the multipart/form-data protocol).

My Code:

test("POST: Upload or Create a file", async ({ request, baseURL }) => {
    const response = await request.post(baseURL + "/v2/files", {
      headers: {
        Accept: "*/*",
        ContentType: "multipart/form-data",
        account: AccountID,
        Authorization: bearer,
      },
      multipart: {
        file: "../../lib/logo.png",
        purpose: "logo",
      },
    });

    const body = JSON.parse(await response.text());
    console.log("this is the status:" + body.status);
    expect(response.status()).toBe(201);

Instead of a 201 status, I receive a 400. Also in debug mode in VS Code, I see:

[ "file should not be null or undefined", ]

Which leads me to believe the file is not being sent or sent incorrectly.

I'm not sure how to structure the file key within multipart, thoughts?

fyt
  • 1
  • 2
  • It seems like there is nothing related to `playwright`, but what seems wrong to me is the file path you have. The problem might be caused by your `tsconfig`'s `baseUrl`. Try defining the path using the `path` module with `path.resolve("lib", "logo.png")`. And log it to make sure it points to the right path. Or use a mocking library like `sinon` to mock the file if `multipart` requires a `File` type input. – archon Mar 09 '22 at 19:59

0 Answers0