[英]fetch() sends lower case header keys
I'm writing an HTTP API library for use in Atom Electron. It is based on fetch
.我正在编写一个 HTTP API 库用于 Atom Electron。它基于fetch
。 The server is outside of my control, but is written in PHP and I can see it checks headers in a case-sensitive fashion.服务器不在我的控制范围内,但写在 PHP 中,我可以看到它以区分大小写的方式检查标头。
My code is something like:我的代码是这样的:
const headers = new Headers();
headers.append('Authorization', `Bearer ${key}`);
const init = {
method: 'GET',
headers: headers
}
const req = new Request(baseUrl + '/items?format=json');
return fetch(req, init);
The request is rejected with a 403 FORBIDDEN
error.请求被拒绝并出现403 FORBIDDEN
错误。 When I look at the request in the Electron Newtork panel, the request headers are present but Authorization
has become authorization
.当我在 Electron Newtork 面板中查看请求时,请求标头存在但Authorization
已变为authorization
。
I know fetch()
is just following the HTTP Standard, but is there a simple way to get fetch()
to send the headers as I supply them?我知道fetch()
只是遵循 HTTP 标准,但是有没有一种简单的方法可以让fetch()
在我提供时发送标头?
Currently fetch will toLowercase()
all headers. 目前fetch将toLowercase()
所有标头。 (there is some discussion here https://github.com/whatwg/fetch/issues/304 about optional disabling). (这里有一些讨论https://github.com/whatwg/fetch/issues/304关于可选的禁用)。
For now you may need to use http://api.jquery.com/jquery.ajax/ with the header
option. 目前,您可能需要使用带有header
选项的http://api.jquery.com/jquery.ajax/ 。
Firefox v110, Chromium v108: Firefox v110,铬 v108:
creating headers with constructor new Headers()
lowers the case, but providing request headers as plain object sends proper capital Athorization: Bearer
使用构造函数new Headers()
创建标头会降低大小写,但提供请求标头作为普通 object 会发送适当的大写Athorization: Bearer
Not tested in other environments未在其他环境中测试
Using fetch myself and doing a similar thing we do as follows... 使用fetch自己做类似的事情我们做如下...
const GLOBALS = require('./Globals');
const HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
const resourceURL = '/some/endpoint'
const body = '';
var request = new Request(`${GLOBALS.API_ENDPOINT}${resourceURL}`, {
method: 'GET',
headers: new Headers(Object.assign(HEADERS, {'Authorization': `JWT ${token}`})),
body: body ? JSON.stringify(body) : null
});
return fetch(request)
.then(res => consume)
Take is as pseudo pseudo code as there are some functional parameters we pass in that are evaluated in the template literal. Take是伪伪代码,因为我们传入了一些在模板文字中计算的函数参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.