API Request Structure and JSON

API Request Structure and JSON

Structure

The structure is divided into 4 parts:-

  1. BaseURL

  2. Endpoints

  3. Query Parameters

  4. Path Parameter

Let's try the above 4 things with an example of any public API using Postman.

NOTE:- I am using App Brewery API. You can try it out on any public API.

BaseURL

BaseURL is something.com. Like in Spotify API, it's https://api.spotify.com

Endpoints

Endpoints can be different for different purposes.

It is a digital location where an API receives requests about a specific resource on its server.

The way of writing endpoints is "BaseURL/Endpoints"

Here it is:- bored-api.appbrewery.com/random
This will return random activity.
You can keep requesting to the API.

Here "random" is an endpoint.

Query Parameters

It is where you can get the key-value pair.

The way of writing it is "BaseURL/Endpoints?query=value"

If you want to write multiple queries you can use "&"

That is "BaseURL/Endpoints?query1=value&query2=value"

It is used when we want to provide some additional information to request and also used for searching, filtering etc.

Here it is:- https://bored-api.appbrewery.com/random?type=relaxation

Here query/key is type and value is relaxation.

It will return the request with the filter type: relaxation.

Path Parameter

It is used to find some specific resource that is dynamic path.

The way of writing it is "BaseURL/Endpoints?path_parameter"

I will be passing a unique ID/key.

Here it is:- https://bored-api.appbrewery.com/activity/4387026

JSON

JSON stands for Javascript Object Notation. It is a way to format data that can be sent over the internet in a readable and also in an efficient way. It is structured after Javascript Object.

In JSON, every key is a string and the value can be a string, array, or number.

//data.json
    {
        "id": 1,
        "title": "Strategy Consulting",
    {
        "id": 2,
        "title": "Management Consulting",
    },

To convert JSON into JS we we will use the "parse method"

const jsData = JSON.parse(data);

To convert JS into JSON we we will use the "stringify method"

const jsonData = JSON.stringify(jsData);

Conclusion

In the above article, you can find the structure of API and some information related to JSON and how you get data by requesting and filtering out things. You can try this out in any public API.