File arriving in larger than expected size in multipart/form-data

We have migrated a large express app to hono + @hono/zod-openapi and everything is great except our multipart/form-data file uploads. The route looks like this:
import { z, createRoute } from '@hono/zod-openapi'
import documentService from 'somewhere'
import { HonoPublicAPIApp } from '../types'
import { routePath } from 'hono/route'

const bodySchema = z.object({
name: z.string(),
file: z.file(),
dateIssued: z.iso.date().pipe(z.coerce.date()).optional(),
dateExpiry: z.iso.date().pipe(z.coerce.date()).optional()
...
})

const route = createRoute({
method: 'post',
path: '/v1/documents',
request: {
body: {
content: {
'multipart/form-data': {
schema: bodySchema
}
}
}
},
responses: {
200: {
description: 'Document created successfully',
content: {
'application/json': {
schema: responseSchema
}
}
}
}
})

export const registerAddDocumentRoute = (app: HonoPublicAPIApp) => {
app.openapi(route, async (c) => {
const form = c.req.valid('form')

const document = await documentService.createDocument({
file: form.file,
name: form.name,
dateExpiry: form.dateExpiry,
dateIssued: form.dateIssued
...
})

return c.json({
data: {
documentId: document.data?.documentId || ''
}
})
})
}
import { z, createRoute } from '@hono/zod-openapi'
import documentService from 'somewhere'
import { HonoPublicAPIApp } from '../types'
import { routePath } from 'hono/route'

const bodySchema = z.object({
name: z.string(),
file: z.file(),
dateIssued: z.iso.date().pipe(z.coerce.date()).optional(),
dateExpiry: z.iso.date().pipe(z.coerce.date()).optional()
...
})

const route = createRoute({
method: 'post',
path: '/v1/documents',
request: {
body: {
content: {
'multipart/form-data': {
schema: bodySchema
}
}
}
},
responses: {
200: {
description: 'Document created successfully',
content: {
'application/json': {
schema: responseSchema
}
}
}
}
})

export const registerAddDocumentRoute = (app: HonoPublicAPIApp) => {
app.openapi(route, async (c) => {
const form = c.req.valid('form')

const document = await documentService.createDocument({
file: form.file,
name: form.name,
dateExpiry: form.dateExpiry,
dateIssued: form.dateIssued
...
})

return c.json({
data: {
documentId: document.data?.documentId || ''
}
})
})
}
The file is correctly available with all the details and content but they problem is that the content appears to be corrupt in some way since the size of the file uploaded is 1.2mb and the file.size in the route handler comes to around 2mb. When I save the file (an image) in S3 and retrieve it it's not consider an image. Note that all the code for saving/retrieving file was working in the express app and is 100% the same. What could be the cause here? Only difference is that in the express app we used multer to parse the request body where as in hono I'm not using anything.
5 Replies
science will prevail
Is there any specific behaviour around multipart/form-data that I'm not aware of? Any tips would be appreciated. Thanks!
ambergristle
ambergristle3w ago
are you sending the file w the right encoding/mime type?
science will prevail
Yes it's the correct mime type, I also thought something might be base64 encoding it but it's not that either.
ambergristle
ambergristle2w ago
rip you could use the S3 sdk directly
science will prevail
You mean presigned url upload? Yea we do that but this is an old area that doesn't see much action.

Did you find this page helpful?