Sub topic 1
Where a Request Carries Data: Path, Query String, and Body
An HTTP request can carry data in three places: the URL path, the query string, and the body. This chapter shows where each one sits in a raw request and what each one is normally used for. Later chapters cover the Spring annotations that read them.
Prerequisites
This chapter assumes you know:
- Basic Java, including classes and records.
- How to run a Spring Boot web project (
spring-boot-starter-web). - Basic HTTP: methods, status codes, and headers.
- Basic JSON syntax.
The anatomy of a request
Every HTTP request has a request line, headers, and an optional body. The request line holds the method and the target. The target holds the path and, optionally, the query string.
curl -v "http://localhost:8080/api/books/42?expand=author" The request this sends looks like this on the wire:
GET /api/books/42?expand=author HTTP/1.1
Host: localhost:8080
Accept: */*Here /api/books/42 is the path. expand=author is the query string, which starts after the ?. This request has no body.
The three places
The path
The path identifies a resource. In /api/books/42, the last segment names one book. Segments that change from request to request are called path variables. In Spring you write them as {id} in a mapping, and a later chapter shows how to read them.
The query string
The query string is a list of name=value pairs joined by &. It follows the ? in the URL. Clients use it to refine a request without changing which resource is addressed: filters, sorting, and paging.
GET /api/books?author=Le%20Guin&page=0&size=10Characters such as spaces must be percent-encoded. Le%20Guin is Le Guin. Query parameters are always text on the wire. Spring converts them to numbers, dates, and other types for you.
The body
The body is the data after the headers. Methods such as POST, PUT, and PATCH usually carry one. The Content-Type header says how to read it. For a JSON API it is application/json.
POST /api/books HTTP/1.1
Host: localhost:8080
Content-Type: application/json
{"title":"A Wizard of Earthsea","author":"Ursula K. Le Guin","pages":183}A body can hold structured, nested data of any reasonable size. A URL cannot do this well.
Comparing the three
| Place | Example | Typical use | Structure | Visible in URL |
|---|---|---|---|---|
| Path | `/api/books/42` | Identify one resource | A single value per segment | Yes |
| Query string | `?page=0&size=10` | Filter, sort, page | Flat name/value pairs | Yes |
| Body | JSON document | Create or update data | Nested, any size | No |
What Spring does with each
Spring MVC reads the request line and body, then fills in method parameters before your controller code runs. Each place has its own annotation:
- Path segments are bound with
@PathVariable. - Query parameters are bound with
@RequestParam. - The body is bound with
@RequestBody.
The following chapters introduce them one at a time, starting with how a controller maps a URL to a method.
Common mistakes
- Putting a large or nested object in the query string. It is hard to encode and can exceed URL length limits.
- Sending a body with
GET. Many servers and proxies ignore or reject it. - Forgetting the
Content-Type: application/jsonheader on aPOST. Spring then cannot choose a JSON reader and replies with415 Unsupported Media Type. - Forgetting to quote a URL containing
&in a shell. The shell treats&as a command separator, so quote the whole URL.
Exercise
A client wants to list books by one author, 10 per page, starting at the second page. Which place should carry the author, the page size, and the page number? Which place should carry the data for a new book?
Show answer
The author, page size, and page number go in the query string, for example ?author=Le%20Guin&size=10&page=1, because they filter and page a collection without changing which resource is addressed. The data for a new book goes in the request body as JSON, sent with POST.
Exercise
A POST to /api/books with a JSON payload returns 415 Unsupported Media Type. Which part of the request should you check first?
Show answer
Check the Content-Type header. It must be application/json so Spring can pick a JSON message reader for the body.