Qwik City - Endpoints
Endpoints are URLs that can be used to retrieve data, redirect users, or otherwise modify the response.
Imagine that you have https://example.com/product/abc123/details
and you want to get the details of the product with the id abc123
. You can retrieve the data in two different ways:
- As HTML for human consumption.
- As JSON for machine consumption.
In both cases, you are retrieving using the same URL https://example.com/product/abc123/details
, the difference is the Accept:
request header.
Accept: text/html
: retrieves HTML for human consumptionAccept: application/json
: retrieves JSON for machine consumption- Perhaps some other serialization format in the future.
In both cases, the logical content of the data retrieved is the same only the formatting is different.
So far we have been talking about components that render static HTML. But what if the component needs data so that it can render it. How do we get the data? Additionally, when rendering on the server we would like to get the data directly, whereas when rendering on the client we would like to get the data from the server in JSON format. A different way to look at it is that the HTML internally retrieves the data from the JSON and then formats it for human consumption.
Endpoints solve the above problem. Endpoints are a way of structuring data retrieval so that it can be used for both servers as well as client rendering inside components.