- Introduction
- Quick start
- Philosophy
- Comparison
- Default behaviors
- Limitations
- Debugging runbook
- FAQ
- Mocking HTTP
- Mocking GraphQL
- Mocking WebSocket
- Integrations
- API
- CLI
- Best practices
- Recipes
Binary responses
Responding with binary data.
You can provide an ArrayBuffer
to the mocked response instance to respond with binary data, like images, video, or audio.
http.get('/images/:imageId', async ({ params }) => {
// Get an ArrayBuffer from reading the file from disk or fetching it.
const buffer = await fetch(`/static/images/${params.imageId}`).then(
(response) => response.arrayBuffer(),
)
return HttpResponse.arrayBuffer(buffer, {
headers: {
'content-type': 'image/png',
},
})
})
Using
HttpResponse.arrayBuffer()
static method will automatically setcontent-length
on the mocked response. Make sure to still set the appropriatecontent-type
header!