Rayrun
← Back to Discord Forum

Playwright API - Request Body

Hi All, I am working on automating a POST request with Playwright. Header body of the request is below json object {

"data": {
    "requestIdFromSource": "1114567",
    "requestDateFromSource": "15/10/2021 10:31:09",
    "sourceId": "P0T2112142M",
    "sourceInstance": "SoftHLA",
    "sourceLocation": "VIC",
    "surname": "AUTO",
    "forename": "TEST",
    "middleName": "x",
    "title": "Mrs",
    "gender": "F",
    "dob": "09/04/1991",
    "homePhone": "xxxxxx",
    "workPhone": "xxxxxxxxx",
   "aboRh": [ {
                "type": "xxx",
                "status": "xxx"
            }],
    "active": false,
    "postTXFlag": true,
    "donorPatientIndicator": "P"
}

}

I tried using property data of request to post the data. But seems I am formatting the data object incorrectly. I have tried passing it as string and then using JSON.Parse to convert to a JSON objectCan you please advise on how I should pass the value for data object properly

const response = await request.post(`${apiURL}persons/person`, {
        headers: {

            Authorization: `Basic ${credentialsBase64}`,
        },
        /
        data: '' "data": {
    "requestIdFromSource": "1114567",
    "requestDateFromSource": "15/10/2021 10:31:09",
    "sourceId": "P0T2112142M",
    "sourceInstance": "SoftHLA",
    "sourceLocation": "VIC",
    "surname": "AUTO",
    "forename": "TEST",
    "middleName": "x",
    "title": "Mrs",
    "gender": "F",
    "dob": "09/04/1991",
    "homePhone": "xxxxxx",
    "workPhone": "xxxxxxxxx",
   "aboRh": [ {
                "type": "xxx",
                "status": "xxx"
            }],
    "active": false,
    "postTXFlag": true,
    "Indicator": "P"
}

}"

});

This thread is trying to answer question "How should I pass the value for the data object properly in a POST request using Playwright?"

0 replies

Answer

Sending POST Requests with Playwright

When sending a POST request with Playwright, you can pass the data object in a few ways.

JSON Data

If your data is an object, Playwright will serialize it to a JSON string and set the content-type header to application/json. Here's how you can do it:

await request.post('https://ray.run/api/createBook', {
  data: {
    title: 'Book Title',
    author: 'John Doe',
  }
});

Form Data

If you need to send form data, use the form option. The data will be encoded with application/x-www-form-urlencoded encoding.

await request.post('https://ray.run/api/findBook', {
  form: {
    title: 'Book Title',
    author: 'John Doe',
  }
});

File Upload

For file uploads, use the multipart option. Provide an object with file information like name, mime type, and content.

await request.post('https://ray.run/api/uploadFile', {
  multipart: {
    file1: fs.createReadStream('/path/to/file1.txt'),
    file2: fs.createReadStream('/path/to/file2.txt'),
  }
});

Remember, the data and form options were added in Playwright version 1.17, and the multipart option in version 1.26. Make sure your Playwright version supports these options.

Related Discord Threads

TwitterGitHubLinkedIn
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 [email protected].