PUT request with tRPC
Hi everyone!
First of all, I tried to find an answer to my question but I couldn’t find one.
I’m trying to update an entity in my database using tRPC stack. For example, I want to edit some of my posts on my website and update their title. Everything works fine but instead of a PUT request, I see a POST request in my network tab.
How can I tell to my tRPC API to send a PUT request instead of a POST request?
Much appreciated!! Thank you in advance
24 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
what if you do a query. does it do a POST as well? out of curiosity
i am quite clueless when it comes to tRPC but it seems that it's the same as in graphQL, queries and mutations. at least in graphQL you only have POST
Let’s assume that you have a blog and you want to create a post.
For that, you’ll have a form on the client side and some inputs for the post title and post body. On submit, you send a POST request, using the useMutation hook. This POST request creates a new resource in your database.
Later that day, you want to edit the post title and again, from the client side, you have a form from which you can edit the title of the post. But for that, you’ll want to send a PUT request in order to update an existing resource. (That blog post that you already have in DB)
The correct way should be to send a PUT request when you create mutations for already existing entities
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
oh okay thanks, but yeah. it seems as in creating and updating is both POST - because they are mutations.
you're using trpc so you don't need to think about http methods. just do a mutation to update stuff
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
@flicknote the thing is that the browser is using a POST request for that 🙂
for mutations it does a post request
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
even though you're doing a mutation to update an existing entity, it will do a post in the browser
this functionality seems identical to GraphQL
here is the code
yeah, but I don't like that 😆
why does it matter?
if you ever hop on to graphql, it's the same there
it's not technically correct :))
i wouldnt spend time thinking about it, seems trivial
yeah, I get it! It's just that it's the first time I'm using tRPC and I've noticed this situation. Everything works fine, I was just curios if I'm missing something 🙂 Thanks for the answers, I am happy living with it just like you said @rre
Just a quick note: If I'm not mistaken you can use https://github.com/jlalmes/trpc-openapi to add meta object to your route and pass
PUT
method in thereGitHub
GitHub - jlalmes/trpc-openapi: OpenAPI support for tRPC 🧩
OpenAPI support for tRPC 🧩. Contribute to jlalmes/trpc-openapi development by creating an account on GitHub.
It's more useful if you want to have public api tho
interesting! I'll take a look at it! Thanks @Piotrek
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yep,
@PogaDev
RPC and REST are two different architectural patterns. In Rest you use the HTTP Methods to describe the business and domains. GET means fetching Data, POST most of the time creating, PUT updating a dataset.
RPC on the other hand is describing your domain with methods ("procedures").
Loading data means sending a request to call "loadData", updating data is sending a request to call "updateData"
To enable this tRPC uses the HTTP Protocol/ Methods aswell to let you execute Code on the server
You would say that a tRPC interface looks like a REST API, but in reality it is just a HTTP API (because we call HTTP APIs most of the time an REST API even when it does not follow the architectural patterns what a REST API really means
Tldr:
REST and tRPC are describing two different approaches to provide an API using HTTP Endpoints so a PUT would be correct for a REST API but is not necessary for tRPC
thanks a lot 😄
oh, got it! thank you for your response
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View