Skip to content

Response headers are lowercased, types are not #6677

@elliottkember

Description

@elliottkember

Describe the bug

Hello!

The types for response.headers is an object with Title Case keys:

headers: RawAxiosResponseHeaders | AxiosResponseHeaders;

AxiosResponse<any, any>.headers: AxiosResponseHeaders | Partial<RawAxiosHeaders & {
    Server: AxiosHeaderValue;
    "Content-Type": AxiosHeaderValue;
    "Content-Length": AxiosHeaderValue;
    "Cache-Control": AxiosHeaderValue;
    "Content-Encoding": AxiosHeaderValue;
} & {
    ...;
}>

However, the actual object has lowercase keys, and this is consistent with the documentation:

  // `headers` the HTTP headers that the server responded with
  // All header names are lower cased and can be accessed using the bracket notation.
  // Example: `response.headers['content-type']`
  headers: {},

To Reproduce

const response = await axios.head(url);
// typed as any, but has a value
const lowercase = response.headers['content-length'];
// correctly typed, but has an undefined value
const titlecase = response.headers['Content-Length'];

In this case, the lowercase type is any and the titlecase type is AxiosHeaderValue | undefined. However, lowercase has the correct value, and titlecase is undefined.

I think this just needs a Lowercase added to the keys of the response type? Should CommonResponseHeadersList all be lower-cased?

Expected behavior

The key types for the response headers should match the actual keys that Axios converts to lowercase.

Sandbox link

https://codesandbox.io/p/sandbox/agitated-sound-9tj77c

Axios version

1.7.7

Node.js Version

20

OS

React Native, but this is a TypeScript thing!

Additional Library Versions

TypeScript v5.6.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue::bugThis issue is related to a bug that requires fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions