Proposal: `Request.json` helper
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
I think the second one is the good approch
Request.json(url, init)
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.
This would require
init.method, as the default method ofGETdoesn't really make a lot of sense here since it's about specifying theContent-Typeof the request body.
Maybe it could be POST by default when using such static method (which would be what ones would expect) ?