← Back to Discord Forum

How to send doc file in an api request

haiderali1091posted in #help-playwright
Open in Discord

I have api automation framework and there is an api request in which I want to send file, how can I do that.

This thread is trying to answer question "How can I send a file in an API request using an API automation framework?"

2 replies

Hi, it depends on the API. Check the API documentation.

I did find these articles, maybe they are of use to you: https://playwrightsolutions.com/making-a-post/ https://medium.com/automation-with-donald/test-api-to-upload-file-using-playwright-framework-4f0bbcf8d2a8 (paid article, unfortunately)

Recent implementation within our Playwright tests involves loading an image file in an API request.

Playwright Test Block:

`test("Test", async ({ gqlRequest, commonUtil, request }) => { const response = await gqlRequest.sendRestRequest("url", "post", await commonUtil.loadImageFile());

const responseJson = await response.json();

expect(responseJson).toHaveProperty("facial_data[0]"); })`

Request Implementation:

`public async sendRestRequest(apiUrl: string, method: string, body: any, authorization: string) { const api = await request.newContext();

const headers = { "content-type": "application/json", Authorization: authorization, };

const requestData = body !== null ? { data: body } : {};

const response = await api[method](apiUrl, { headers: headers, ...requestData, });

return response; } `

Loading Image File:

async loadImageFile() { const fileName = path.resolve("src/app.healthcheck.dataprovider/", "DAMODHARAN KOTHANDARAMAN.png"); return fs.readFileSync(fileName); }

Please customize it based on your need

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.