即使将凭证设置为true,axios也不会发送标头/ cookie数据

时间:2020-10-16 10:22:00

标签: javascript node.js axios

我没有使用快递服务器,而是在Node中运行我的应用程序。

在此过程中,我正在向URL发送一个请求,以便检索随后发出该请求所需的cookie和标头。

但是,第二个请求失败,因为它没有被发送上一个响应的标头/ cookie。

这是我的完整代码:

const client = axios.create({
  withCredentials: true,
  proxy: {
    host: "192.168.0.137",
    port: 9393,
  }, //Fiddler proxy
});
client
  .post("https://auth.riotgames.com/api/v1/authorization", {
    acr_values: "urn:riot:bronze",
    claims: "",
    client_id: "riot-client",
    nonce: 1,
    redirect_uri: "http://localhost/redirect",
    response_type: "token id_token",
    scope: "openid link ban lol_region",
  })
  .then(() => {
    client
      .put("https://auth.riotgames.com/api/v1/authorization", {
        type: "auth",
        username: "testuser1",
        password: "testpassword1",
        remember: false,
        language: "en_GB",
        region: "EUW1",
      })
      .then((response) => {
        console.log(response);
      })
      .catch((err) => {
        console.log(err.response);
      });
  });

如您所见,我正在使用一个提琴手代理,以便查看正在发送的标头。

这些是我从第一个响应中收到的标头:

HTTP/1.1 200 OK
Date: Fri, 16 Oct 2020 10:20:19 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 31
Connection: close
Set-Cookie: __cfduid=db46ba3cac5fd35ddf03f3b3819d24fac1602843619; expires=Sun, 15-Nov-20 10:20:19 GMT; path=/; domain=.riotgames.com; HttpOnly; SameSite=Lax
cache-control: no-store, no-cache, must-revalidate, proxy-revalidate
etag: W/"1f-+CND8IPJ6eMbm0SU/bb85j/SB6c"
expires: 0
pragma: no-cache
surrogate-control: no-store
vary: Accept-Encoding
x-content-type-options: nosniff
x-download-options: noopen
CF-Cache-Status: DYNAMIC
cf-request-id: 05d285c8d90000003971b81000000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
X-RiotGames-CDN: Cloudflare
Set-Cookie: did=9c1d9dff853c4ec59c6e0608d5f17c6d; Max-Age=31536000; Path=/; Expires=Sat, 16 Oct 2021 10:20:19 GMT
Set-Cookie: asid=_9gHl9TckOeZTpDI_Rpj19q3xIjPsTYqLc0R2k5GFBk.g%2BDgPnqvXbA%3D; Path=/; HttpOnly; Secure
Set-Cookie: clid=ec1; Path=/; HttpOnly; Secure
Set-Cookie: __cf_bm=65c675be473c63ab14bd318503ee53867d03155e-1602843619-1800-AQofj7huRNdHZaAWwN+WiQwSFv+jAuget1G0wVUTD5MQbqBkuACkj3nb7+E8kfAR/IImwtA1+gEqhjCL7C/3bGc=; path=/; expires=Fri, 16-Oct-20 10:50:19 GMT; domain=.riotgames.com; HttpOnly; Secure; SameSite=None
Server: cloudflare
CF-RAY: 5e310bee2f090039-MAN

然后在第二个请求中,我查看了标头,而这些标头都没有被发送:

PUT /api/v1/authorization HTTP/1.1
Accept: application/json, text/plain, */*
Content-Type: application/json;charset=utf-8
User-Agent: axios/0.20.0
Content-Length: 117
host: auth.riotgames.com
Connection: close

对此我感到非常困惑。为什么不存储cookie?我已将withCredentials设置为true,所以它应该可以工作?

1 个答案:

答案 0 :(得分:1)

好的,所以我通过手动方式解决了这个问题。我不再依赖Axios来存储和发送cookie,而是简单地检索了“ set-cookie”标头,并在下一个请求中手动设置了cookie:

const cookies = response.headers["set-cookie"];
client
  .put(
    "https://auth.riotgames.com/api/v1/authorization",
    {
      type: "auth",
      username: "testuser1",
      password: "testpassword1",
      remember: false,
      language: "en_GB",
      region: "EUW1",
    },
    {
      headers: {
        Cookie: cookies,
      },
    }
  )