We generally use internet for browsing websites via a Web browser. Several applications also use internet for connecting with server and exchanging data. For example, Uber app needs a server to store vehicles data. When the app is launched, it connects to the server and fetches data about nearby vehicles. When a ride is requested, server is informed about ride details to notify the drivers.
How does this data exchange work on internet? Through well defined protocols / rules. Each protocol has its own connection mechanism and request response formats. One such widely used protocol is HTTP (Hypertext Transfer Protocol). All web browsers use HTTP to request webpages. Applications & Server also use HTTP APIs for communication. URLs are used to locate a resource on the server. For example,
https://google.com
requests the Google homepage on Google servers. Response format will be an HTML file which can be rendered by a Web browser.- For an Uber like app,
https://my-uber.com/vehicles?location=23.4,74.5
might request for vehicles data near the specified location. This is an example of Web API which is used for data exchange between server and client. Here response need not be HTML but raw data, which has to be processed and then displayed by the application. Response formats can be JSON, XML, CSV, etc.
HTTP Request Methods
HTTP Request Methods
HTTP supports multiple request methods.
GET request
GET request
The most basic HTTP request method is GET. GET request is used to simply fetch the data located by a URL. Web browsers send a HTTP GET request when a webpage is requested. Examples :
-
HTML response :
GET HTTP/1.1 Host : google.com 200 OK <!DOCTYPE html> <html> ... </html>
-
JSON response :
GET /vehicles?location=23.4,74.5 HTTP/1.1 Host : my-uber.com 200 OK { "status": "success", "data": { "vehicles": [ {...}, {...}, {...} ] } }
POST request
POST request
Another widely used HTTP method is POST. In contrast to GET, POST is used to submit a data payload along with requesting data. It is commonly used for log in purpose to send username and password to the server. Example :
-
Login
POST /login HTTP/1.1 Host : thestreamliners.in Content-Type: application/json // Payload : { "username": "alpha123" "password": "FJF5@454%KDJF" } // Response : 200 OK <!DOCTYPE html> <html> ... </html>
-
Uber ride request
POST /ride HTTP/1.1 Host : my-uber.com Content-Type: application/json // Payload : { "service": "Sedan" "destination": {...} } // Response : 200 OK { "status": "success" }
There are more HTTP request methods but we shall work with GET and POST methods only. You can learn more about HTTP request methods here.
HTTP Status Codes
HTTP Status Codes
Along with response, servers send HTTP Status code also. It is a numeric value used to know the response status. Following are the most commonly used HTTP Status Codes :
200
- OK : Successful request404
- Not Found : Resource not found500
- Internal Server Error
You can learn more about HTTP response status codes here.
Let us now learn how to send HTTP requests to Web APIs programmatically.