A User Guide for Rest API

 


The full form of API is Application Programming Interface. This is a collection of rules that helps different programmes in communicating with each other and extracting data from one another.

In simple words when you give any command to the application it gives data to the server, and the server retrieves the data do an interpretation of the data and work according to it. Finally, return the readable information for you. All these procedures are performed by API.

The full form of REST is Representational State Transfer. It is a collection of rules that an API developer follows when the developer creates an API so that users can get the data which is called a resource or a response when they enter a URL link which is called a request.

Structure of a URL (request)

The request is consists of an endpoint or a route, a method, headers and a body. The endpoint also has various parts. The root-endpoint is the initial part of a request. For example ‘https://api.facebook.com’ is the root-endpoint of Facebook’s API.

The second part of endpoint is path which tells the response the user is looking for. For example, if a user wants to use Ad library API of Facebook to know the ads stored in ad library of Facebook the URL the user will use is ‘https://api.facebook.com/ads/library/api’, ‘/ads/library/api’ is the path.

The list of paths of APIs which are available for the user is present in a document called API documentation or repository. Sometimes, the  path contains ‘:’ which denotes variable in the path.

The final part of the endpoint is query parameters although query is not a part of REST but several API uses it. Query parameters are used to modify request which key value pair. They start with ‘?’ and the parameter pair is separated by ‘&’. Example: ‘?query1&query2’.

Trailing Endpoints with cURL:

Users can test the API endpoint or a request in any programming language using several tools available. JavaScript user can use Fetch API, Python users can use Python Requests, Ruby users can use Ruby’s Net::HTTP class, and cURL is a widely used tool as it is made for REST API.

How to use cURL

First install the latest version of cURL if the user has cURL then the user can check the version of the tool by using the command line “curl-version” as cURL works only on command lines.

To test various endpoints, users have to give the command as: curl root-endpoint. For example a user wants API documentation of twitter’s API, the user will give a command ‘curl https://api.twitter.com’.

If users want the user repository available for users they can add /users to the command given above. Users can also add query parameters by adding /?query1=value1&query2=value2 after path. After the command users will get the API documentation as a response.

Focusing on other parts of URL

The method is a request you send to the server and the method is of five types. The first is GET, and is used when user wants any data. The second one is POST, and is used to create a new entry in a database. The third one is PUT and PATCH and is used to update a database.

The last one is DELETE, and is used to delete a data. You can send a method with cURL like this example, ‘curl –x GET https://api.twitter.com/user/repos’.

The header is another part of the request. It helps to provide the information about the authentication and body content to the server and the user. A proper list of headers can be extracted using “HTTP Header’s Reference”.

Readers can share HTTP headers with curl in the following way:  For example, a header that inform the sever to expect JSON content ‘curl-H”Content-Type:application/json”https://api.twitter.com’.

Users can view the header like this, for example ‘curl-H”Content-Type:application/json”https://api.twitter.com -v’. 

The Body is the final part of the request which contains the information user want to send to the server. The data can be shared through cURL in the following manner: “curl –x POST <URL> -d property1=value1 –d property2= value2”.

The user can send the data to the server using the method POST, PUT, PATCH or DELETE only. Generally cURL shares the data by default but if the user wants to share the JSON data, they can do it in this way:  curl –x POST https://requestb.in?1ix963n1 –H “Content-Type: application/json –d’{“property1”=”value1”, “property2”=”value2”}.

What is JSON

JSON is a format for sending data through REST API. The full form of JSON is JavaScript Object Notation as JSON object is like JavaScript Object.

How to authenticate the server

The method like POST, PUT, or DELETE can alter the data so authentication is required. So, there are two ways to authenticate the server. First is username and password and the second is secret token.Secret token involves authentication through social media sites.

The user can authenticate the server by username and password through curl like this, for example, ‘curl –x POST –u “username: password” https://api.twitter.com/user/repos’.

HTTP status code and error message

Sometimes, users can get the error messages such as require authentication, problems parsing JSON, etc. the error message means there is some problem in the request. If user writes –I inside of –x the error message will appear.

There are some HTTP status code that can indicate the status of the response. Some of the codes are as follows:

200+: The request has succeeded.

300+: The request is redirected.

400+: The error occurred from the side of client.

500+: The error occurred from the server.

API up gradation

The user can upgrade the API version in two ways. First is directly in the endpoint and the second is in a request header. The example for the first one is ‘https://api.twitter.com/1.1/account/settings.json’.

The example for the second one is ‘curl https://api.twitter.com-H Accept:application?vnd.twitter.v1.1+json’.the ACCEPT header can be used for API up gradation. 

       

Written by – Bhavana Thakur