Thursday, March 9, 2023

Example of a REST API

Here is an example of how a REST API might work:

Let's say you are building a weather app and you want to show the current temperature for a given location. You could create a REST API that allows the weather data to be accessed using HTTP requests.

Assume the weather data is stored in a database, with each record having a location name and a temperature value. This is an example of how the API might be designed:

  • To retrieve the current temperature for a particular location, you would send a GET request to the following endpoint:
https://api.example.com/weather/<location>

The <location> parameter would be replaced with the name of the location you want to retrieve the temperature for.

  • The server would respond with a JSON object containing the temperature for the requested location. For example:
{
  "location": "New York",
  "temperature": 75.4
}
  • To create a new record for a location with a given temperature, you would send a POST request to the following endpoint:
https://api.example.com/weather

with a JSON payload containing the location and temperature values. For example:

{
  "location": "San Francisco",
  "temperature": 67.8
}
  • To update an existing record's temperature value for a location, you would send a PUT request to the endpoint:
https://api.example.com/weather/<location>

with a JSON payload containing the updated temperature value. For example:

{
  "temperature": 72.1
}
  • To delete a record for a location, you would send a DELETE request to the endpoint:
https://api.example.com/weather/<location>

In summary, a REST API is a way of exposing a data source using HTTP requests to retrieve, create, update or delete records. The data returned by the API is usually in a standardized format like JSON, making it easy for client applications to consume the data.


No comments:

Mimmo97 Blog Archive