Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Last active January 29, 2021 14:19
Show Gist options
  • Save kettanaito/4cc22814d0f7e4af30d44f451bd7f9f1 to your computer and use it in GitHub Desktop.
Save kettanaito/4cc22814d0f7e4af30d44f451bd7f9f1 to your computer and use it in GitHub Desktop.
Response body retrieval
// Determine a body of the given Response.
// In case of a Response that has no body (i.e. 204 response),
// return `null`.
function getResponseBody(res: Response) {
res.body // ReadableStream, locked
await res.text() // always "" for a response with no body
}
// Empty string cannot be used as a response body
// for the responses that have no body (i.e. 204 response).
new Response('', { status: 204 }) // TypeError
// The correct way to construct such response
// would be to pass `null` as its body:
new Response(null, { status: 204 })
// However, there is no way to retrieve that `null` body
// from a given response instance.
getResponseBody(responseWith204StatusCode) // ???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment