- From: Luca Casonato <notifications@github.com>
- Date: Thu, 27 Jan 2022 08:50:44 -0800
- To: whatwg/fetch <fetch@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/1389@github.com>
The `Response` object currently has a `Response.redirect` helper method to create `Response` objects with preset `Location` headers. `Request` and `Response` already have a `json` prototype method that allows parsing bodies as JSON, but there is currently no trivial way to **create** JSON `Response` objects directly from a POJO. I propose the addition of a `Response.json(obj, opts)` static method that creates a `Response` object with a body string containing the JSON serialized representation of `obj`. `opts` would allow further modification of the JSON response, namely setting headers and status code. By default the status code is 200, and the headers are the default response headers + `content-type: application/json`. ```webidl interface Response { [NewObject] static Response json(any object, optional ResponseInit init = {}); } ``` The motivation for this addition is that in server side runtimes using WHATWG `Request` and `Response` objects, folks often create helper methods for this behaviour that need to be duplicated across each project. This being built in would be very useful. Thoughts? Implementer interest: - Chromium :grey_question: - Firefox :grey_question: - Safari :grey_question: - Deno :heavy_check_mark: - Cloudflare Workers :grey_question: -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/fetch/issues/1389 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/fetch/issues/1389@github.com>
Received on Thursday, 27 January 2022 16:50:56 UTC