T
TanStack3w ago
foreign-sapphire

createServerFn POST vs GET

It seems like the docs don't specify when to use POST vs GET for a server function as you can technically write whatever logic you want inside and it gets serialized automagically using either. Wanted to know if there's any specific guidance here other than just using your best judgement to determine whether it should be GET vs POST? Also why is it limited to just GET and POST only?
3 Replies
helpful-purple
helpful-purple3w ago
@Harsh , this is a confusion that came up some time ago The server function is not intended for use as a regular REST API. It is a function to be called be the front end, but is ensured that will be executed in the server, not in the browser. It is useful to utilize server resources and increase security by hiding the APIs being called, keys, etc. I personally, for security reasons, prefer to always use POST instead of GET, and I think it could be a standard in createServerFn (allowing to change to GET if needed, but I don't see a scenario). Resuming: the server function is almost a "transparent" layer to force functions to be executed from the server side, reducing the amount of code on the client side. The server functions themselves can call API functions, for example, but don't mix the concepts: server functions are not intended to be used as API replacements.
foreign-sapphire
foreign-sapphireOP3w ago
Oh I see, yes I guess I was conflating the two as the same. My bad. Are there scenarios/technical reasons where server functions would be more useful than just normal api routes or is it sort of a preferences thing based on the structure/layout of the application?
helpful-purple
helpful-purple3w ago
A server function is a function: you call it directly or using the useServerFn hook. An API endpoint must be fetched.

Did you find this page helpful?