gpt4 book ai didi

javascript - 如何在 header 中正确提供身份验证 token ,以便 graphql 不会重定向到登录页面?

转载 作者:行者123 更新时间:2023-12-01 00:16:18 25 4
gpt4 key购买 nike

在我的 QA 组织中,我被要求构建一个 JavaScript 测试文件以使用 k6 (loadimpact) 调用我们的 graphQL 端点。我正在成功生成我们的身份验证 token 并将其添加到我正在调用的调用的 header 中。但由于某种原因,我被重定向到我们公司的登录页面。在不重定向的情况下进行此调用的正确方法是什么?

我正在创建我的标题:

let headers = {
'Authorization': `Bearer ${authToken}`,
"accept": "*/*",
"Origin": "https://myurl.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"DNT": "1",
"content-type": "application/json" };

我对 graphQL 的调用:

let myResponse = http.post("https://myurl.com/load/graphql", 
"{..variables...query..}",
{headers: headers});

即使生成的身份验证代码有效,我也会收到如下返回结果:

{
"remote_ip": "myIP",
"remote_port": 443,
"url": "https://companyURL/login/",
"status": 200,
"proto": "HTTP/1.1",
"headers": {
"Cache-Control": "public, max-age=0",
"Last-Modified": "Wed, 18 Sep 2019 17:47:24 GMT",
"Etag": "W/\"527-16d457c67e0\"",
"Server": "Microsoft-IIS/10.0",
"Content-Length": "1319",
"Content-Type": "text/html; charset=UTF-8",
"Accept-Ranges": "bytes",
"Vary": "Origin",
"X-Powered-By": "Express, ASP.NET",
"Date": "Tue, 14 Jan 2020 15:13:05 GMT",
"Access-Control-Allow-Credentials": "true"
},
"cookies": {},
"body": "**...My company page login HTML...**",
"timings": {
"duration": 26.9992,
"blocked": 0,
"looking_up": 0,
"connecting": 0,
"tls_handshaking": 0,
"sending": 0,
"waiting": 26.9992,
"receiving": 0
},
"tls_version": "tls1.2",
"tls_cipher_suite": "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"ocsp": {
"produced_at": 0,
"this_update": 0,
"next_update": 0,
"revoked_at": 0,
"revocation_reason": "",
"status": "unknown"
},
"error": "",
"error_code": 0,
"request": {
"method": "POST",
"url": "https://myurl/load/graphql",
"headers": {
"Origin": [
"https://myurl"
],
"Dnt": [
"1"
],
"Content-Type": [
"application/json"
],
"User-Agent": [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
],
"Authorization": [
"Bearer myreallylongauthcode"
],
"Accept": [
"*/*"
]
},
"body": "...env variables..query..",
"cookies": {}

}}

我已经回顾了与 graphQL 相关的其他问题,但似乎没有什么是非常合适的。在 loadimpact 站点上查看 K6 文档时,它显示将身份验证代码添加到 header ,但这显然不起作用。

最佳答案

您应该像这样传递它:http.post("https://myurl.com/load/graphql", {..variables...query..}", {headers: headers});

更多示例:https://docs.k6.io/docs/params-k6http

关于javascript - 如何在 header 中正确提供身份验证 token ,以便 graphql 不会重定向到登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59737760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com