Hypertext Transfer Protocol (HTTP) is a protocol that provides a standardized way for computers to communicate with each other. It has been the foundation for data communication over the internet since 1990 and is integral to understanding how client-server communication functions.
When a request is sent, the client opens the connection; once a response is received, the client closes the connection. The client and server only maintain a connection during the response and request. Future responses are made on a new connection.
There is no dependency between successive requests. Each request is an individual request
Utilizing headers and cookies, sessions can be created to allow each HTTP request to share the same context.
Any type of data can be sent over HTTP as long as both the client and server know how to handle the data format. In our case, we'll use JSON.
Universal Resource Identifiers (URIs):
An example URI is https://github.com/manisaiprasad?tab=repositories It has certain components:
specifies the protocol used to access the resource, HTTP or HTTPS. In our example HTTPS.
specifies the host that holds the resources. In our example github.com.
specifies the specific resource being requested. In our example, /manisaiprasad.
an optional component, the query string provides information the resource can use for some purpose such as a search parameter. In our example, tab=repositories.
HTTP requests are sent from the client to the server to initiate some operation. In addition to the URL, HTTP requests have other elements to specify the requested resource.
Method: Defines the operation to be performed
Path: The URL of the resource to be fetched, excluding the scheme and host
optional information, success as Accept-Language
Body: optional information, usually for methods such as POST and PATCH, which contain the resource being sent to the server
Different request methods indicate different operations to be performed. It's essential to attend to this to correctly format your requests and properly structure an API.
GET: ONLY retrieves information for the requested resource of the given URI
POST: Send data to the server to create a new resource.
PUT: Replaces all of the representation of the target resource with the requested data
PATCH: Partially modifies the representation of the target resource with the requested data
DELETE: Removes all of the representation of the resource specified by the URI
OPTIONS: Sends the communication options for the requested resource
After the request has been received by the server and processed, the server returns an HTTP response message to the client. The response informs the client of the outcome of the requested operation.
Status Code & Status Message
Headers: similar to the request headers, provides information about the response and resource representation. Some common headers include:
Content-Type: the media type of the body of the request
Body: optional data containing the requested resource
As an API developer, it's important to send the correct status code. As a developer using an API, the status codes—particularly the error codes—are important for understanding what caused an error and how to proceed.
1xx Informational (100's)
2xx Success (200's)
3xx Redirection (300's)
4xx Client Error (400's)
5xx Server Error (500's)
304: Not Modified
400: Bad Request
404: Not Found
405: Method Not Allowed
500: Internal Server Error