[英]Why do I get error code 401 when sending post request
From the following request I get status code 302 redirect.从以下请求中,我得到状态代码 302 重定向。 But I want to send cookies I've received from the response with the redirect to the next page.但我想发送 cookies 我从响应中收到的重定向到下一页。 Right now I get status code 401 when sending the request, and I've figured out that it's because I need to send the cookies along with the redirect, but I don't get the cookies until I fetch the url that gives me the redirect.现在我在发送请求时收到状态码 401,我发现这是因为我需要将 cookies 与重定向一起发送,但我没有得到 cookies,直到我获取给我重定向的 Z572D4E421E5E6B9711 . How do I do that?我怎么做?
async function login (url) {
let request = await fetch(url, {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
body: 'username=name&password=pass&submit=login',
})
return request
}
In general body data type must match the Content-Type header.通常正文数据类型必须匹配 Content-Type header。 so in your case you have to use所以在你的情况下你必须使用
body: new URLSearchParams({
'userName': 'name',
'password': 'pass',
'grant_type': 'password'
}),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.