openapi-react-query-codegen icon indicating copy to clipboard operation
openapi-react-query-codegen copied to clipboard

How do I access the response headers?

Open atresnjo opened this issue 1 year ago • 8 comments

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!

atresnjo avatar Nov 04 '24 06:11 atresnjo

@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
      },
    }
  );

7nohe avatar Jan 20 '25 22:01 7nohe

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

trabati avatar Apr 02 '25 04:04 trabati

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.

trabati avatar Apr 07 '25 10:04 trabati

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.

seriouslag avatar Apr 07 '25 15:04 seriouslag

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

trabati avatar Apr 07 '25 15:04 trabati

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

@seriouslag do you have any working example to share?

thanks

el0i404 avatar May 15 '25 11:05 el0i404

same issue, need to read Content-Disposition for filename

sahandsn avatar Aug 06 '25 20:08 sahandsn

@seriouslag please stop spreading false information. This is the second time you’ve claimed something that doesn’t exist and misled people in the wrong direction.

trabati avatar Aug 21 '25 10:08 trabati