Introduction to HTTP
Introduction to HTTP
- 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.
- HTTP is a protocol that allows the fetching of resources, such as HTML documents.
- It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser.
- A complete document is reconstructed from the different sub-documents fetched, for instance, text, layout description, images, videos, scripts, and more.
Features:
Connectionless: 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.
Stateless: There is no dependency between successive requests.
Not Sessionless: Utilizing headers and cookies, sessions can be created to allow each HTTP request to share the same context.
Media Independent: Any type of data can be sent over HTTP as long as both the client and server know how to handle the data format.
Elements:
Universal Resource Identifiers (URIs):
- An example URI is http://www.example.com/tasks/term=homework.
It has certain components:
Scheme: specifies the protocol used to access the resource, HTTP or HTTPS. In our example HTTP.
Host: specifies the host that holds the resources.
- In our example www.example.com.
Path: specifies the specific resource being requested.
- In our example, /tasks.
Query: an optional component, the query string provides information the resource can use for some purpose such as a search parameter.
- In our example, /term=homework.
Side Note: URI vs URL:
- You may be unsure what the difference is between a URI (Universal Resource Identifier) and a URL (Universal Resource Locator).
- These terms tend to get confused a lot, and are even frequently used interchangeably—but there is a distinction.
- The term URI can refer to any identifier for a resource—for example, it could be either the name of a resource or the address of a resource (since both the name and address are identifiers of that resource).
- In contrast, URL only refers to the location of a resource—in other words, it only ever refers to an address.
- So, "URI" could refer to a name or an address, while "URL" only refers to an address.
- Thus, URLs are a specific type of URI that is used to locate a resource on the internet when a client makes a request to a server.
HTTP Requests:
- 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.
Elements:
Method: Defines the operation to be performed.
Path: The URL of the resource to be fetched, excluding the scheme and host.
HTTP Version: HTTP/2.0
Headers: 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.
Request Methods:
- 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.
Methods:
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 request data.
PATCH: Partially modifies the representation of the target resource with the request data.
DELETE: Removes all of the representation of the resource specified by the URI.
OPTIONS: Sends the communication options for the requested resource.
HTTP Responses:
- 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.
Elements:
Status Code & Status Message.
HTTP Version.
Headers: similar to the request headers, provides information about the response and resource representation.
Some common headers include:
- Date
- Content-Type: the media type of the body of the request
Body: optional data containing the requested resource.
Status Codes:
- 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.
Codes fall into five categories:
- 100 Informational
- 200 Success
- 300 Redirection
- 400 Client Error
- 500 Server Error
Common Codes:
- 200: OK
- 201: Created
- 304: Not Modified
- 400: Bad Request
- 401: Unauthorized
- 404: Not Found
- 405: Method Not Allowed
- 500: Internal Server Error
Comments
Post a Comment