With Playwright, you can send HTTP(S) POST requests and get responses. It's smart enough to follow redirects and manage cookies for you.
Here's how you can send JSON objects directly:
await request.post('https://ray.run/api/login', {
data: {
username: 'myUsername',
password: 'myPassword'
}
});
If you're dealing with form data, use the form
option. This will encode your data into the request body with application/x-www-form-urlencoded
encoding:
await request.post('https://ray.run/api/login', {
form: {
username: 'myUsername',
password: 'myPassword'
}
});
Need to upload files? Use multipart/form-data
encoding. You can pass a file-like object or open a file as a stream:
// Pass file content directly as an object
await request.post('https://ray.run/api/uploadAvatar', {
multipart: {
avatarField: {
name: 'avatar.png',
mimeType: 'image/png',
buffer: Buffer.from('<file-content>')
}
}
});
// Open file as a stream and pass it to the post method
const stream = fs.createReadStream('/path/to/avatar.png');
await request.post('https://ray.run/api/uploadAvatar', {
multipart:{
avatarField :stream
}
});
That's it! You're now a pro at sending POST requests with Playwright. For more tips and tricks, check out our blog.
If you still have questions, please ask a question and I will try to answer it.
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].