Overview

Appendix C. A quick introduction to HTTP

HTTP is the communication protocol that web applications use to let a client and server exchange information in a structured way. It is designed as a stateless, text-based request-response model, which means each request stands on its own and the data is exchanged as plain text. For developers building web apps with Spring, understanding these basics is enough to follow the examples and work effectively without needing to memorize the full HTTP specification.

An HTTP request is made up of a mandatory URI and method, plus optional parameters, headers, and body content. The URI identifies the resource the client wants to work with, while the method describes the action to take, such as GET for reading data, POST for creating data, PUT for replacing data, and DELETE for removing data. Other methods like OPTIONS and PATCH appear less often but still matter, and the choice of method should match the intended behavior of the operation. Small pieces of data are commonly sent as request parameters or headers, while larger payloads are placed in the request body.

An HTTP response always includes a status code, and may also include headers and a body. Status codes beginning with 2 indicate success, 4 indicate a client-side problem such as a bad request or missing resource, and 5 indicate a server-side failure. Because HTTP is stateless, servers sometimes use sessions to remember information across multiple requests from the same client, such as items added to a shopping cart. A session is identified with a session ID and lasts only for a limited time before a new one must be created.

Figure C.1 HTTP is a protocol that describes how a client and a server talk. HTTP assumes a client makes a request, and the server responds. The protocol describes what the client’s request and the server’s response look like. HTTP is stateless, meaning the requests are independent of one another, and text-based, which means the information is exchanged as plain text.
Figure C.2 The HTTP request URI identifies the resource the client requests to work with. The first part of the URI identifies the protocol and the server that runs the server app. The path identifies a resource exposed by the server.
Figure C.3 For an online shop, the backend needs to identify the clients and remember the products they added to their carts. The HTTP requests are independent one from another, so the backend needs to find another way to remember the products added by each client.
Figure C.4 The HTTP session mechanism. The server identifies the client with a unique session ID it generates. The client sends the session ID in the next requests, so the backend app knows which memory location it reserved earlier for the client.

FAQ

What is HTTP in a web application?HTTP is a stateless, text-based, request-response protocol that describes how a client communicates with a server in a web app.
Why is HTTP important for Spring applications?Most applications built with frameworks like Spring are web apps, and web apps use HTTP to exchange data between client and server.
What are the main parts of an HTTP request?An HTTP request includes a request URI, a request method, and optionally request parameters, request headers, and a request body.
What does the request URI do?The request URI identifies the resource on the server that the client wants to work with. It typically includes the server location, port, and resource path.
What HTTP methods are used most often?The most common methods are GET to obtain data, POST to add data, PUT to change data, and DELETE to remove data.
When should request parameters or the request body be used?Use request parameters for small pieces of data and the request body for larger amounts of data. Both are optional and used only when needed.
What are the main parts of an HTTP response?An HTTP response includes a response status, and optionally response headers and a response body.
What do HTTP status code ranges 2xx, 4xx, and 5xx mean?2xx means the request was processed successfully, 4xx means there was a problem with the client’s request, and 5xx means the server encountered a problem while processing the request.
What is the purpose of the HTTP session?An HTTP session lets the server store data across multiple requests from the same client, which is useful when requests need to be correlated, such as in an online shopping cart.
How does a client keep the same HTTP session?The server gives the client a session ID, and the client sends that session ID in a request header on later requests so the server can recognize the same session.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free