Skip to content

Request interceptor can not set header with name contains digits. #5582

@xpol

Description

@xpol

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions