MerlinM
tRPC6mo ago
12 replies
Merlin

Did anyone succeed at file upload with the fastify adapter?

Hi there,
I spent literally hours trying to figure out how to get file upload with multipart/form-data working using the fastify adapter. I couldn't get it to work.

The docs (https://trpc.io/docs/server/non-json-content-types) specifically tell not to handle multipart beforehand, which is why I didn't register @fastify/multipart before registering the tRPC plugin. This however leads to Fastify rejecting multipart as non-parseable, so I tried the following:

app.addContentTypeParser(
  "multipart/form-data",
  (_req, body, done) => done(null, body) // hand off to tRPC
);


After that I get a tRPC Error "400 BAD_REQUEST: Failed to parse body as FormData."

Btw, if I do register @fastify/multipart, I get the same error. Also I'm using httpLink, so that's not the issue.
Is there any working example for fastify? I only found one using express which is a way simpler framwork in general.

Thanks in advance.
In addition to JSON-serializable data, tRPC can use FormData, File, and other Binary types as procedure inputs
Non-JSON Content Types | tRPC
Was this page helpful?