huma icon indicating copy to clipboard operation
huma copied to clipboard

How to return files over output?

Open stargeneration-winston opened this issue 8 months ago • 2 comments
trafficstars

In echo, I usually do this:

c.Response().Header().Set("Content-Type", "application/pdf")
if err := generatePDFInvoice(c.Request().Context(), params, c.Response().Writer); err != nil {
  return err
}

return nil

How do I respond with files in body in Huma? The docs do not have such an example

stargeneration-winston avatar Mar 21 '25 04:03 stargeneration-winston

@stargeneration-winston take a look at https://huma.rocks/features/response-outputs/#body. You can use a type of []byte for the body if the file fits in memory, otherwise you can use https://huma.rocks/features/response-streaming/ to get the response writer.

https://go.dev/play/p/IV3P5XEJrHV

Hope that helps!

danielgtaylor avatar Mar 21 '25 15:03 danielgtaylor

@stargeneration-winston take a look at https://huma.rocks/features/response-outputs/#body. You can use a type of []byte for the body if the file fits in memory, otherwise you can use https://huma.rocks/features/response-streaming/ to get the response writer.

https://go.dev/play/p/IV3P5XEJrHV

Hope that helps!

So I need to send file responses are streaming responses?

stargeneration-winston avatar Mar 24 '25 05:03 stargeneration-winston