fetch icon indicating copy to clipboard operation
fetch copied to clipboard

Proposal: `Request.json` helper

Open denis-migdal opened this issue 1 year ago • 3 comments

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 :

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

denis-migdal avatar Dec 07 '24 14:12 denis-migdal

I think the second one is the good approch

Request.json(url, init)

Shivangbijalwan avatar Dec 16 '24 18:12 Shivangbijalwan

This would require init.method, as the default method of GET doesn't really make a lot of sense here since it's about specifying the Content-Type of the request body.

rossipedia avatar Feb 19 '25 18:02 rossipedia

This would require init.method, as the default method of GET doesn't really make a lot of sense here since it's about specifying the Content-Type of the request body.

Maybe it could be POST by default when using such static method (which would be what ones would expect) ?

denis-migdal avatar Feb 19 '25 20:02 denis-migdal