Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将XMLHttpRequest的setRequestHeader读取到API中?_Javascript_Python_Google Chrome Extension_Fastapi - Fatal编程技术网

Javascript 如何将XMLHttpRequest的setRequestHeader读取到API中?

Javascript 如何将XMLHttpRequest的setRequestHeader读取到API中?,javascript,python,google-chrome-extension,fastapi,Javascript,Python,Google Chrome Extension,Fastapi,我有一个程序——chrome扩展——向我在Heroku-FastAPI、Python上托管的API发送API调用。我在XML API调用中使用了包含唯一用户ID的setRequestHeader(),并希望从我的API中读取头-因此我可以将每个唯一用户ID头的调用限制为每秒1次 var req = new XMLHttpRequest(); req.open("GET", checkDomain, true); req.setRequestHeader("Autho

我有一个程序——chrome扩展——向我在Heroku-FastAPI、Python上托管的API发送API调用。我在XML API调用中使用了包含唯一用户ID的setRequestHeader(),并希望从我的API中读取头-因此我可以将每个唯一用户ID头的调用限制为每秒1次

var req = new XMLHttpRequest();
req.open("GET", checkDomain, true);
req.setRequestHeader("Authorization", "Basic " + userid);
(req.send(null);
然后js代码稍后读取API端点中始终为空的响应,该端点返回用户的头

FastAPI端码-

 #Reads header?
 @app.get("/items/")
 async def read_items(user_agent: Optional[str] = Header(None)):
     return {"User-Agent": Header}
我从chrome扩展端解析并发出警报的返回值始终是{“用户代理”:“}空对象,但成功通过

有更好的方法

您可以在
request
对象中找到有关请求的所有信息

来自fastapi导入请求的

@app.get(“/items/”)
异步def读取项目(请求:请求):
returnrequest.headers
这包含了关于请求头的所有信息,如果您只想获得用户代理,您可以简单地执行此操作

return request.headers['user-agent']