2. HTTP request and response

Different Types of API

  • REST API

When you hear or read about APIs, you'll almost always see the term REST associated with it. Remember, REST is just a standard for APIs, and by now most APIs conform to the REST standard.

REST is a set of rules that developers follow when they create their API. When an API follows the rule of Representational State Transfer; they are called as REST API. One of these rules states that you should be able to get a piece of data (called a resource) when you hit on a specific URL.

REST determines how the API architecture looks like. Based on Uniform Resource Identifiers (URIs) and HTTP protocol, REST-based APIs use JSON for data formatting which is considered to be browser-compatible. We will be using REST based API throughout our course.

  • SOAP APIs

This is a protocol that uses XML as a format to transfer data. Its main function is to define the structure of the messages and methods of communication. It provides higher levels of security and makes them better at accuracy as compared with the REST-based APIs in the way messages are exchanged.

  • GraphQL APIs

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

Now, time to learn about the different components of API. Are you excited ?

excited-minions-gif.gif

HTTP Request


HTTP (Hypertext Transfer Protocol) is basically a set of rules for transferring files such as text, sound, images, video and other type files over the web. While HTTP Request is the call that is made to server to request data.

As discussed in the last module, there are multiple type of HTTP Request method through which you can transfer data to/from server.

HTTP request is in form of a request message which includes 3 main components.

  • Request line
  • Request Headers
  • Message Body ( Optional )

Request line

Request line contains request method, request URI and protocol version. And, it looks something like this.

GET /api/users/dipakkr HTTP/1.1

Request Header

A request header is an HTTP header that can be used in an HTTP request to provide information about the request context. You can send information like client device Information from where the request is being made, type of response you can accept etc. You can also include authorization or authentication token required by server in request headers.

GET /users HTTP/1.1
User-Agent: Chrome/4.0
Host: api.github.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-type : application/json

HTTP Response

Every time you send a request to a REST API, it returns some form of response to tell you what happened to the request you sent.

Every response has head section, called as Response Header ( similar to request header) and content section which contains the data provided by resource ( server )

Screenshot 2021-09-10 at 2.51.14 AM.png

These response header you see in the image above tells about the state of response.

  • HTTP/2 200 - Here, 200 is the HTTP response status code which tells the response is success.
  • Server as nginx, tells about type of server.
  • Content Type tells about the data format for response so that you can use right parser to fetch response.
  • Any headers beginning with X- are custom headers, and are not included in the HTTP spec. eg . x-proxy-cache

You can run this command in your terminal to make curl request. Here, -i denotes to show

curl -i https://hplussport.com/api/products

Endpoints


What if you want to talk to me? You will have to dial in my phone number +91-99xxxxxxxx

What if you want to talk to an API? You will have to connect to a specific location! That specific location is an endpoint.

Endpoints tell you the different places you can connect to an API.

They are URLs and the end of each URL connects you to specific data options from the API. Take a look back to flipkart.com. What if you want to view a specific catalogue like television, mobile, AC etc?

As soon as you tap on the product you are interested to buy; you are redirected to a URL. Those URLs are the end-points.

  • flipkart.com/tv
  • flipkart.com/mobile
  • flipkart.com/ac

Query Parameters


Now once you have tapped on the product you are looking to buy; what if you want to see products of a LG brand only.

You would most probably apply brand filter. What happens next? As soon as you apply the LG filter; a request is made to the server again & now you can see only TV of LG brand.

Parameters allow us to more narrowly define or filter the response that we get.

They're added onto the URL following the endpoint.

Parameters are denoted by a question mark.

  • flipkart.com/tv?brand=lg
  • flipkart.com/tv?brand=lg&ratings=4+
  • flipkart.com/tv/brand=lg/ratings=4+

Note ➖ You may also see forward slashes used for parameters but I believe it's more intuitive to use a question mark.

3.5 Body Data

Trivia - Can you guess the scenario, where you need to use body data ?

You will need to send body data with requests whenever you need to add or update some data on server. That's where body in request message is required.

A simple example can be, let's say you are creating a new Gmail account, so you are literally telling Google to take your information and create my gmail account. Typically you will use body data with PUT, POST, and PATCH requests.

We will learn more about how to make Post request with a body in postman in next module.