question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fastify adapter: Custom content-type parser for application/json is breaking normal REST api requests on a fastify server

See original GitHub issue

https://github.com/trpc/trpc/blob/d5808b863490f95ff76defce4886833b11672e88/packages/server/src/adapters/fastify/fastifyTRPCPlugin.ts#L25-L31

This works just fine for tRPC endpoints but breaks other REST api endpoints as request.body is a string where it’s expected to an object. This can be easily reproduced on the official fastify examples by creating a REST POST endpoint and sending some data inside body.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
soulsam480commented, Mar 31, 2022

Yes exactly. I think if we can match the fastify-plugin type, we don’t need to encapsulate it and can use it directly. Thanks for the quick fix.

1reaction
skarab42commented, Mar 31, 2022

@soulsam480 I found the problem, I am working on a fix…

Reading the docs:

As with the other APIs, addContentTypeParser is encapsulated in the scope in which it is declared. This means that if you declare it in the root scope it will be available everywhere, while if you declare it inside a plugin it will be available only in that scope and its children.

I probably broke the encapsulation system with the use of fastify-plugin, I’m reworking the plugin to allow its use with or without fastify-plugin.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ContentTypeParser - Fastify
If you need to support different content types, you can use the addContentTypeParser API. The default JSON and/or plain text parser can be...
Read more >
fastify example doesn't allow POST to pages/api handler #9978
Bug report Describe the bug Performing a POST to an API route when using a fastify server wrapper results in 400 Invalid body...
Read more >
Fasity Complete Course - learn how to build REST API's
I will show you how you can build a rest Apis using a framework called fastify which is a nodejs framework and it...
Read more >
Streaming Files | NestJS - A progressive Node.js framework
To do this with Nest, normally you'd do the following: ... Fastify, by default, can support sending files without needing to call stream.pipe(res)...
Read more >
GraphQL Code Libraries, Tools and Services
import RequestParser from "@graphql-box/request-parser"; ... GraphQL server and a hypermedia API supporting modern REST formats (JSON-LD, JSONAPI...): <?php.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found