-
Notifications
You must be signed in to change notification settings - Fork 382
Responds with Blob and custom content-type from API Lambdas #7288
-
Hey!
I have a functionality to respond from my api with auto-generated simple graphic.
What I do:
- I created a lambda (get).
- I took
useHonoContextand call:
const context = useHonoContext()
context.res.headers.set('Content-Type', 'image/png')
- 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!
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
Found solution:
const context = useHonoContext()
context.res = new Response(new Uint8Array(graphic).buffer, {
status: 200,
headers: {
'Content-Type': 'image/png',
},
})
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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', }, })
Beta Was this translation helpful? Give feedback.
All reactions
-
Hey, @zllkjc
I think the direct Response support will be the best and clean solution.
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1