gpt4 book ai didi

dart - DART HttpRequest不为CORS OPTIONS请求提供授权 header

转载 作者:行者123 更新时间:2023-12-03 02:57:40 26 4
gpt4 key购买 nike

我尝试使用HttpRequest(dart:html)进行POST请求,以调用通过基本身份验证保护的rest服务。

HttpRequest request = new HttpRequest(); // create a new XHR
var url = "http://localhost:8082/xyz";
request.open("POST", url, async: false); // POST the data to the server

String username = "foo";
String password = "bar";
final auth = CryptoUtils.bytesToBase64(UTF8.encode("$username:$password"));

request.setRequestHeader('authorization',"Basic $auth");
request.setRequestHeader('content-type',"application/json");
request.setRequestHeader("accept", "application/json");
String jsonData = '{"language":"dart"}'; // etc...

request.send(jsonData); //exception 401 Unauthorized

在执行POST调用之前,将执行没有授权 header 的OPTIONS调用(由dart:html发出)。这将导致401未经授权的响应。

Request header:

Accept:*/*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Access-Control-Request-Headers: authorization, content-type, accept
Access-Control-Request-Method: POST
Cache-Control: max-age=0
Connection: keep-alive
Host: localhost:8082
Origin: http://localhost:63342
Referer: http://localhost:63342/dart_client/test/all_test.html
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.0 (Dart) Safari/537.36



响应头:

Content-Length: 0
Date: Mon, 02 Feb 2015 23:33:58 GMT
Server: Jetty(9.2.7.v20150116)
WWW-Authenticate: basic realm="xyzrealm"



有没有一种方法可以将授权 header 提供给OPTIONS调用?

任何建议都很好。谢谢

最佳答案

OPTIONS请求是由浏览器自动发出的,您不能修改该请求。服务器需要在不进行身份验证的情况下允许OPTIONS预检请求。

参见http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0
用户凭证不包括在内。

关于dart - DART HttpRequest不为CORS OPTIONS请求提供授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28303249/

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