Because HTML forms don't allow HTTP requests like PUT or DELETE, only GET and POST, I'm sending the PUT request using the fetch API. And then I'm redirecting the page using location.assign() with the Location header from the 302 server response.
const res = await fetch('https://example.org/test', {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data)
}).catch(err => console.error(err));
if (res.status >= 300 && res.status < 400) {
window.location.assign(res.headers.get("Location"));
}
The problem is that the new redirected request is also a PUT request, while I would like that window.location.assign would use a GET request. Why does it use the previous HTTP verb of PUT and how can I prevent that?
CodePudding user response:
I figured it out on myself.
It's not that window.location.assign()
uses the last HTTP verb. It's that fetch()
will automatically follow the redirects. It has the option redirect which has as default option "follow". There isn't a pretty solution to follow redirects yourself with a different HTTP verb, since the redirect status basically means that the same resource has to be accessed through a different URL. The best solution is thus to let the server respond with a status of 200 Ok and give instructions on how to access the next page. (I just set the Location header).