Introduction to HTTP

JunePyo Suh·2020년 5월 13일
0

Overview of HTTP

  • TCP/IP based application layer protocol that allows web-based applications to communicate and exchange data.
  • A stateless, connectionless protocol that can deliver any data. Client and server do not have any knowledge of previous requests and responses.
    - Server won't even know that a re-visiting user has already logged in. In order for a server to distinguish a logged in user, the server generates a web token to the user during the login process, whose browser will save the token in its cookie storage.
    - Later, the user's browser will combine token information stored in cookies (and sometimes sessions) in the header section of its request messages to access website information, indicating to the server that the user had logged in.
    • Likewise, to access any information regarding the progress of previous requests and responses, we should use cookies and sessions as HTTP will be powerless.

HTTP is convenient in that it can quickly and reliably transfer data on the web. The request-response cycle works on the web via http messages.

HTTP Message Structure

All contain plain text-based information composed of three sections: (1) status line, (2) headers, (3) body. A request Http message differs from a response Http message.

Request HTTP message

1. Start line: Method + Request Target(URI) + HTTP Version

  • A method is a command that tells the server what to do
    - GET, POST, PUT, DELETE, etc.
  • URI is a set of readable characters to locate the resources the client is requesting
    - path/to/file.ext format
  • HTTP/1.0

2. Headers: Key value pairs that contain additional information about the request(META data), including token value and cookies

  • Host: the address of the server to which the client is sending the request
    - www.mysebsite.com
    • the information provided for this key is combined with the detailed endpoint address provided by the Host key to locate a specific address
  • User-Agent: information about the client's browser --> user-agent contains various information, ranging from which computer the user is using
  • Accept: text/html
    - tells the server what type of file the request is asking for
  • Accept Encoding: states which content encoding the client is able to understand
  • Content-type: states what type of data is contained in the request message body
  • Accept-language: en-us

3. Body: the actual message/content from the request. The body could be in various formats -- json, html, xml, etc.

Response Http message

1. Status line: http/version + status code + status text

  • status code tells the client if the request successed or failed
    - 200 OK: is successful
    • 301 Moved Permanently: Target URI has been moved to a different address
    • 400 Bad Request: incoming request is an invalid request; happens usually when input values in body are incorrect
    • 401 Unauthorized: the user needs to log in or register first before being authorized by the server to access the page
    • 403 Forbidden: the user has no access to the request; for instance, only the users who paid for the content can access the server
      • the difference between 401 and 403 is that 403 can still occur for users that have logged in, if for instance they are users without paid plans
    • 404 NOT FOUND: there is no matching server for the provided uri
    • 500 Internal Server Error: an error has occurred in the server

2. Headers: Host, Accept, Accept-language, etc.

3. Body: contains the requested file

  • products/myproduct.html

HTTP Methods

1. GET

The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

**While Get requests do not have body, they can still pass certain parameters via query strings & query parameters through URI.
ex) www.wecode.com/api?category=1

2. HEAD

Same as GET, but transfers the status line and header section only.

3. POST

A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

4. PUT

Replaces all current representations of the target resource with the uploaded content.

5. DELETE

Removes all current representations of the target resource given by a URI.

6. CONNECT

Establishes a tunnel to the server identified by a given URI.

7. OPTIONS

Describes the communication options for the target resource.

  • For instance, if you want to know what request methods can the server process in certain uri, use OPTIONS to ask the server
  • http -v OPTIONS http://example.org

8. TRACE

Performs a message loop-back test along the path to the target resource.

HTTP Methods explained in tutorials point

  • In case of SPAs (single-paged applications), back end system delivers various status codes on requests, with which the front end system renders various pre-prepared pages.

0개의 댓글