Understanding the basics of REST API with HTTP and JSON



What is an API? The Messenger 
API stands for Application Programming Interface.The API is the messenger that takes REQUESTS and tells the system what you want to do and returns a RESPONSE back to you. This allows two pieces of software to communicate with each other.
Metaphor example: Waiter at a restaurant gets the REQUEST from customer and tells the Kitchen what to do and then delivers the RESPONSE back to the customer.
One of the most popular types of API is REST or, as they’re sometimes known, RESTful APIs.

What is REST ? Let us use HTTP requests to format those messages 
The short answer is that REST stands for Representational State Transfer. Architecture style for designing network applications which are are stateless, meaning that the server does not need to know anything about what state the client is in and vice versa. In other words REST is a set of rules/standards/guidelines that follow the REST specification, basically governs the behaviour of clients and servers.  This is typically done through HTTP requests and returns data in JSON format.

What is REST API?
Simply put the API uses REST to access HTTP requests to either GET, POST, PUT or DELETE data.

HTTP Request
This stands for hypertext transfer protocol (HTTP). The purpose of the HTTP protocol is to provide a standard way for web browsers and servers to talk to each other to transfer data between a web server and your computer. In order for images, text or pages to be delivered to your browser, it needs to request to fetch that data and uses a HTTP request to do exactly that. The server(REST API) then responds (here's the file, sorry you don't have access etc) along with the file if everything went well and permission was given.

HTTP is an application protocol that runs on top of the TCP(Transmission Control Protocol)/IP(Internet Protocol) suite of protocols. When these HTTP requests and responses are being sent, they use TCP/IP to reduce and transport information in small packets of binary sequences of ones and zeros that are physically sent through electric wires, fiber optic cables and wireless networks.
Here is how it works:



Communication in HTTP (Hyper Text Transfer Protocol) center around a concept called the Request-Response Cycle, which looks like this:



To make a valid request, the client needs to include four things:
1. URL ( Uniform Resource Locator)
2. Method
3. List of Headers
4. Body
URL
URLs become an easy way for the client to tell the server which things it wants to interact with, called resources.
Method
The method request tells the server what kind of action the client wants the server to take in. The four most commonly seen in API’s are:
GET — Asks the server to retrieve a resource.
POST — Asks the server to create a new resource.
PUT — Asks the server to edit/update an existing resource.
DELETE — Asks the server to delete a resource.
Headers
Headers provide meta-information about a request.
Body
The request body contains the data the client wants to send the server.

Here is a simple volley http request with headers example in Android(Kotlin):



Here is a simple volley http request example in Android(JAVA):




JSON Format
This stands for JavaScript Object Notation which is is a lightweight format for storing and transporting data. JSON is most commonly used in asynchronous HTTP requests, once a HTTP request has got data it returns it to the client in JSON format, this is because the REST architecture allows API providers to deliver data in multiple formats such as plain text, HTML, XML, YAML, and JSON, which is one of its most loved features. Thanks to the increasing popularity of REST, the lightweight and human-readable JSON format has also quickly gained traction, as it’s super suitable for quick and painless data exchange.
The two primary parts that make up JSON are keys and values. Together they make a key/value pair. In the below image the key is "firstName" and the value is "John" also the key is "lastName" and the value is "Doe". if you iterate through the JSON you can access any keys/values you require.






Comments

Popular posts from this blog

Big O asymptotic analysis/Big O notation

Basic Overview of Microsoft Azure?