Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Responds with Blob and custom content-type from API Lambdas #7288

Answered by RuBAN-GT
capybaraborn asked this question in Q&A
Discussion options

Hey!
I have a functionality to respond from my api with auto-generated simple graphic.

What I do:

  1. I created a lambda (get).
  2. I took useHonoContext and call:
const context = useHonoContext()
context.res.headers.set('Content-Type', 'image/png')
  1. The function returns a Blob.

But the summary get result still responds with Content-Type: application/json and my blob was converted to the string.
I tried to create a middleware and mutate headers but my result - the same.

How I can return dynamic data with non json type?

Thanks!

You must be logged in to vote

Found solution:

const context = useHonoContext()
context.res = new Response(new Uint8Array(graphic).buffer, {
 status: 200,
 headers: {
 'Content-Type': 'image/png',
 },
})

Replies: 2 comments 2 replies

Comment options

I found a "hack" from middleware:

 c.header('Content-Type', 'image/png')
 const graphic = await service.generate()
 return c.body(graphic as any)

But the same doesn't work from lambda and actually is better to use lambda scopes in order to have the clean logic. Is it possible?

You must be logged in to vote
0 replies
Comment options

Found solution:

const context = useHonoContext()
context.res = new Response(new Uint8Array(graphic).buffer, {
 status: 200,
 headers: {
 'Content-Type': 'image/png',
 },
})
You must be logged in to vote
2 replies
Comment options

Nice haha, I didn't even think of this method. But this method is not very compatible with our code, but it works fine in hono.

Btw, what do you think of if we support directly returning a response:

const context = useHonoContext()
return new Response(new Uint8Array(graphic).buffer, {
 status: 200,
 headers: {
 'Content-Type': 'image/png',
 },
})
Comment options

Hey, @zllkjc
I think the direct Response support will be the best and clean solution.

Answer selected by capybaraborn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /