二进制响应

使用二进制数据进行响应。

你可以为模拟响应实例提供 ArrayBuffer,以便使用二进制数据(例如图片、视频或音频)进行响应。

¥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',
    },
  })
})

使用 HttpResponse.arrayBuffer() 静态方法将自动在模拟响应上设置 content-length。请确保仍然设置了适当的 content-type 标头!

¥Using HttpResponse.arrayBuffer() static method will automatically set content-length on the mocked response. Make sure to still set the appropriate content-type header!