Skip to content

How do I access the response headers? #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
atresnjo opened this issue Nov 4, 2024 · 6 comments
Open

How do I access the response headers? #168

atresnjo opened this issue Nov 4, 2024 · 6 comments

Comments

@atresnjo
Copy link

atresnjo commented Nov 4, 2024

I have a mutation which returns a Content-Disposition header with the filename, etc. But my generated mutation returns only a string, any way to get access to the response headers? Thanks for the great package!

@7nohe
Copy link
Owner

7nohe commented Jan 20, 2025

@atresnjo

The mutations generated by this library do not return headers. Instead of using the generated mutation hook, create your own hook.
The following is an example using the axios client. (The problem is that the types generated by the Hey API do not have headers, though.)

import { addPet } from "../openapi/requests/services.gen";
import {
  AddPetMutationResult,
  UseFindPetsKeyFn,
} from "../openapi/queries";
import { AxiosError } from "axios";
import { AddPetData, AddPetError } from "../openapi/requests/types.gen";

// create your own hook
const { mutate, isError } = useMutation<
    AddPetMutationResult,
    AxiosError<AddPetError>,
    AddPetData
  >({
    mutationKey: [useAddPetKey],
    mutationFn: (options) => addPet(options),
  });


// mutation
mutate(
    {
      body: { name: text },
    },
    {
      onSuccess: (data) => {
           const contentDisposition = data.headers["content-disposition"];
           // do something
      },
    }
  );

@trabati
Copy link

trabati commented Apr 2, 2025

@7nohe are there any plans to expose the response headers on the queries and mutartions at some point?

@trabati
Copy link

trabati commented Apr 7, 2025

What is the reason for hiding the headers from the result of the queries and mutations?

Headers can carry important information, like x-total-count for pagination.

@seriouslag
Copy link
Collaborator

seriouslag commented Apr 7, 2025

You can get headers in the generated response by describing the headers as part of the response on the API spec.

https://swagger.io/specification/ see Response Object and the Header Object.

@trabati
Copy link

trabati commented Apr 7, 2025

@seriouslag
Could you please further explain that, or do you have an example?
I don't get how that should work.

@eloiweb3
Copy link

eloiweb3 commented May 15, 2025

@7nohe Having the same issue. Need access to headers response.

@seriouslag do you have any working example to share?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants