[whatwg/fetch] Proposal: `Request.json` helper (Issue #1791)

### What problem are you trying to solve?

Currently, we have a `Response.json(obj, init)` static method to build JSON `Response`, cf #1389

However, we do not seem to have a `Request.json(url, obj, init)` equivalent to build JSON `Request`.

### What solutions exist today?

Setting `body` with `JSON.stringify` as well as the `content-type` header by hand :

```js
new Request(url, {
    body: JSON.stringify(obj),
    headers: {
         "content-type": "application/json"
    }
});
```

### How would you solve it?

I suggest adding a `Request.json(url, obj, init)` static method mirroring the `Response.json(obj, init)` static method.

Alternatively, it could be a `Request.json(url, init)` static method (the JS object then being given through init.body).

### Anything else?

_No response_

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/1791
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/1791@github.com>

Received on Saturday, 7 December 2024 14:19:25 UTC