【问题标题】:How to pass withCredentials option to Request?如何将 withCredentials 选项传递给 Request?
【发布时间】:2018-11-29 07:57:57
【问题描述】:

根据CORS specwithCredentials: true 可以在 XHR 或 Fetch 请求中设置,以在飞行前请求中包含授权凭据。

我正在使用请求库(在浏览器中)。我怎样才能通过该属性?像这样简单地在选项中包含 {withCredentials} 似乎不起作用:

request.get({
   ...,
   auth: {...},
   withCredentials: true
})

【问题讨论】:

  • “根据CORS specwithCredentials: true 可以在 XHR 或 Fetch 请求中设置,以在飞行前请求中包含授权凭据。” — 实际上,这不是 CORS 规范所说的。规范说,授权凭证必须永远在预检请求中发送。请参阅fetch.spec.whatwg.org/#ref-for-credentials%E2%91%A5,其中说,“CORS 预检请求从不包含凭据”
  • 嗯。我真的很困惑。如果 credentials: 'include' 的飞行前请求中未包含凭据,那么……该标志甚至有什么作用?
  • 该标志指定在您自己的代码的实际 GET 请求中包含凭据。我将在回答您的其他问题时对此进行扩展

标签: cors xmlhttprequest http-authentication preflight node-request


【解决方案1】:

为了回答我自己的问题,我已经确认 Request 库确实支持 withCredentials: true 选项(或者更确切地说,它的一个依赖项支持),并且它最终被转换为传递给的 credentials: "include" 参数fetch().

正如上面的 cmets 所述,that 的行为不是我所期望的,但问题不在于 Request。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多