A resource to learn about APIs in general and different types of https requests
Answered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
I am looking to understand api requests in a deeper level. the extent of my knowledge about apis is that there could be an api for app that allows you to access it's data, this data can also be filtered by changing the url, the returned data is usually json and you can use it however you want. but in nextjs there seems to be also api routes which I couldn't understand at all, like what's the difference between an api request and using
another thing I am having trouble wrapping my head around is https requests and their different types. I once took a php course and it said that POST is for sending secret data and GET is for getting data in the URL. but there seems to be more to it
both of these things seem to be assumed knowledge acording to the nextJS docs and I want to understand them well
fetch("something.com/api") inside a normal page.another thing I am having trouble wrapping my head around is https requests and their different types. I once took a php course and it said that POST is for sending secret data and GET is for getting data in the URL. but there seems to be more to it
both of these things seem to be assumed knowledge acording to the nextJS docs and I want to understand them well
Answered by joulev
### about the first question:
### about the second question
but they are at best just hints so the backend running the API knows what to do when it receives the request. while following best practice is good, you are not required to use them all. for example graphql just uses
there could be an api for app that allows you to access it's dataso nextjs api routes are exactly the same. it is an interface for apps to interact with your data.
what's the difference between an api request and using fetch("something.com/api") inside a normal page.
fetch("something.com/api") is an HTTP request to an API route of the app at something.com### about the second question
but there seems to be more to ityes. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
but they are at best just hints so the backend running the API knows what to do when it receives the request. while following best practice is good, you are not required to use them all. for example graphql just uses
POST for everything, and uses the request body to determine the action to take. trpc on the other hand just uses GET for reads and POST for writes, and ignores everything else.9 Replies
@Saltwater Crocodile I am looking to understand api requests in a deeper level. the extent of my knowledge about apis is that there could be an api for app that allows you to access it's data, this data can also be filtered by changing the url, the returned data is usually json and you can use it however you want. but in nextjs there seems to be also api routes which I couldn't understand at all, like what's the difference between an api request and using `fetch("something.com/api")` inside a normal page.
another thing I am having trouble wrapping my head around is https requests and their different types. I once took a php course and it said that POST is for sending secret data and GET is for getting data in the URL. but there seems to be more to it
both of these things seem to be assumed knowledge acording to the nextJS docs and I want to understand them well
### about the first question:
### about the second question
but they are at best just hints so the backend running the API knows what to do when it receives the request. while following best practice is good, you are not required to use them all. for example graphql just uses
there could be an api for app that allows you to access it's dataso nextjs api routes are exactly the same. it is an interface for apps to interact with your data.
what's the difference between an api request and using fetch("something.com/api") inside a normal page.
fetch("something.com/api") is an HTTP request to an API route of the app at something.com### about the second question
but there seems to be more to ityes. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
but they are at best just hints so the backend running the API knows what to do when it receives the request. while following best practice is good, you are not required to use them all. for example graphql just uses
POST for everything, and uses the request body to determine the action to take. trpc on the other hand just uses GET for reads and POST for writes, and ignores everything else.Answer
@joulev ### about the first question:
> there could be an api for app that allows you to access it's data
so nextjs api routes are exactly the same. it is an interface for apps to interact with your data.
> what's the difference between an api request and using fetch("something.com/api") inside a normal page.
`fetch("something.com/api")` *is* an HTTP request to an API route of the app at `something.com`
### about the second question
> but there seems to be more to it
yes. <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods>
but they are at best just hints so the backend running the API knows what to do when it receives the request. while following best practice is good, you are not required to use them all. for example graphql just uses `POST` for everything, and uses the request body to determine the action to take. trpc on the other hand just uses `GET` for reads and `POST` for writes, and ignores everything else.
Saltwater CrocodileOP
so nextjs api routes are exactly the same. it is an interface for apps to interact with your data.so fetch is for using others people's apis and api routes and for creating my own apis?
yes
@joulev yes
Saltwater CrocodileOP
is there any reason for me to create my own api if there aren't any other people using it
@Saltwater Crocodile is there any reason for me to create my own api if there aren't any other people using it
if you want to use client-side rendering with react-query for example, then you need the api for your own frontend to fetch
but for an appdir application, you probably dont need apis yes
Saltwater CrocodileOP
for example reddit has it's api for third party apps to use. but in most app cases there wouldn't be a need for other people to to use my api, yet I see many tutorials using api routes despite that
@joulev but for an appdir application, you probably dont need apis yes
Saltwater CrocodileOP
oh I get it now, tysm
@Saltwater Crocodile for example reddit has it's api for third party apps to use. but in most app cases there wouldn't be a need for other people to to use my api, yet I see many tutorials using api routes despite that
the frontend needs a way to access your data. there are two ways:
* server-side rendering which is the way used by e.g. php, react server component, etc.: the server fetches the data and renders it to html then sends that html to the browser.
* client-side rendering which is the way used by SPAs: the server simply sends a skeleton prebuilt html to the browser, then the browser makes secondary requests to the apis on the server to get the data
those tutorials most likely follow that second method
* server-side rendering which is the way used by e.g. php, react server component, etc.: the server fetches the data and renders it to html then sends that html to the browser.
* client-side rendering which is the way used by SPAs: the server simply sends a skeleton prebuilt html to the browser, then the browser makes secondary requests to the apis on the server to get the data
those tutorials most likely follow that second method