Last active
January 29, 2021 14:19
-
-
Save kettanaito/4cc22814d0f7e4af30d44f451bd7f9f1 to your computer and use it in GitHub Desktop.
Response body retrieval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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