简体   繁体   English

为什么我在发送 post 请求时收到错误代码 401

[英]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.

相关问题 为什么我在尝试 POST 请求(表单)时会出错? - Why do i get error when trying to POST request (form)? 发送 axios 获取带有 Auth 标头的请求时出现 401 错误 - 401 Error when sending axios get request with Auth Headers 提取POST请求时出现405错误。 为什么“获取”请求确定后我不能发出POST请求? - 405 error when fetching POST request. Why I cant do a POST request when Get request is OK? 为什么Rails jQuery POST请求出现401错误? - Why am I getting 401 error for a Rails jQuery POST request? 为什么要发布JSON文件HTTP REQUEST时出现错误400 - Why do I get a Error 400 when I want to post a JSON file HTTP REQUEST 运行此脚本时出现错误代码401,该怎么办? - I get the Error code 401 when I run this script, what can I do? 为什么在尝试发送带有标题的请求时出现错误? - Why i get error when try post request with headers? 向节点 js 发送发布请求时,我得到一个空的 object - when sending a post request to node js, I get an empty object 使用redmine API进行POST请求时出现401错误,即使我已经包含了api密钥 - Getting 401 error when using redmine API for a POST request even though I have included the api key 当OPTIONS请求具有statusCode 200时,为什么在API Gateway GET请求上出现CORS错误? - Why do I get a CORS error on API Gateway GET request when the OPTIONS request has statusCode 200?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM