-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Description
Describe the bug
I'm trying to add a request interceptor with the following code:
myInterceptor = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
config.headers.set('a1', 'value');
return config;
};
It turns out is has no effect.
I believe it is due to the incorrect isValidHeaderName
function https://github.com/axios/axios/blob/v1.x/lib/core/AxiosHeaders.js#L32-L34:
function isValidHeaderName(str) {
return /^[-_a-zA-Z]+$/.test(str.trim());
}
And been called with https://github.com/axios/axios/blob/v1.x/lib/core/AxiosHeaders.js#L103-L107
} else if(utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
setHeaders(parseHeaders(header), valueOrRewrite);
} else {
header != null && setHeader(valueOrRewrite, header, rewrite);
}
Because isValidHeaderName(header)
returns false
, so it goes to setHeaders(parseHeaders(header), valueOrRewrite);
logic where parseHeaders(header)
gets nothing form a1
string.
Actually isValidHeaderName(header)
should return true
for a1
and goto the else branch:
header != null && setHeader(valueOrRewrite, header, rewrite);
which will set the a1
header correctly.
Accroding to rfc9110:
- https://www.rfc-editor.org/rfc/rfc9110.html#name-fields
- https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens
digits are allowed in header names.
To Reproduce
Run the below code here: https://runkit.com/embed/3ic1gajcu2yn
Code snippet
const {AxiosHeaders} = require("axios")
const headers = new AxiosHeaders();
headers.set('a1', 'value')
headers.get('a1') // -> undefined
Expected behavior
Expected the header been successfully set. And headers.get('a1') returns value
.
Axios Version
1.3.4
Adapter Version
No response
Browser
No response
Browser Version
No response
Node.js Version
14
OS
No response
Additional Library Versions
No response
Additional context/Screenshots
No response