Lesson 3 of 5
What is in a request
Listen to an AI summary of What is in a request
A request has a fixed layout, and each part of the layout has a name.
The example is one order from the sports shop.
One order from the sports shop
Sam, a customer, signs in to the sports shop website and opens the page for a hoodie. Sam chooses the size Large, and the URL of the page changes to shop.example/hoodie?size=large.
The hoodie page has a form with two boxes, and each box is called a form field. Sam types 1 in the form field named quantity and SK in the form field named initials.
- A web page at the address shop.example/hoodie?size=large.
- Hoodie
- A form field labelled Quantity, holding 1. the form field named `quantity`
- A form field labelled Initials, holding SK. the form field named `initials`
- A button that says Order. the button that sends the request
The request the browser sends
Sam presses Order, and the browser sends the request to the server.
The first line
POST /hoodie?size=large HTTP/1.1
- POSTthe method
- /hoodiethe path
- size=largea query parameter
- HTTP/1.1the version of HTTP
The method is the first word of a request and says what the request is for. The method GET asks to read a page. The method POST sends data to the server, such as an order.
The path is the part of a URL after the name of the website. The path says which page or thing the request is for. The browser copies the path and the query parameter from the URL.
A query parameter is a named value at the end of a URL, after a question mark. A value is one piece of data, such as the number 7 or the text Sam. The query parameter size=large has the name size and the value large.
Headers and cookies
A header is a named line near the top of a request that holds extra information. A header has a name, then a colon, then a value.
A cookie is a value that a server gives a browser. The browser sends the cookie back with every later request to the same website, in a header named Cookie. The sports shop gave Sam's browser the cookie session=7f3a9c when Sam signed in. The lesson on sessions covers how a server uses a cookie to know who is signed in.
The body
The body is the last part of a request and holds the main data. The body of Sam's request holds the two form fields, joined by an & sign.
How JavaScript code reads a request
Input is data that comes into a program from outside the program. Four parts of a request hold input as named values. The four parts are the query parameters, the headers, the cookies and the body. The path holds input too. In the path /orders/1042, the sender chose the number 1042.
The four lines of JavaScript run on the server of the sports shop. Each line reads one value from Sam's request into a variable, a named place that holds a value. Look at the word after req. on each line.
const size = req.query.size // a query parameter const quantity = req.body.quantity // a form field in the body const session = req.cookies.session // a cookie const browser = req.headers['user-agent'] // a header
The word const makes a variable. The variable req holds the whole request. req.query holds the query parameters, and req.body holds the form fields from the body. req.cookies holds the cookies, and req.headers holds the headers.
const size = req.query.size
- const sizea variable
- reqthe request
- querythe query parameters
- sizethe name of one query parameter
size.The name after the last full stop picks one value by name. After the first line runs, the variable size holds the text large.
The names query, body, cookies and headers come from Express, which is ready-made JavaScript code for receiving requests. Express gives code a value from the path in req.params.
The same four parts in Python
Every programming language has its own names for the same four parts. Python is another programming language that servers run. In the Python, the variable request holds the request.
- A query parameter
req.query.sizein JavaScript.request.args["size"]in Python.- A form field in the body
req.body.quantityin JavaScript.request.form["quantity"]in Python.- A cookie
req.cookies.sessionin JavaScript.request.cookies["session"]in Python.- A header
req.headers['user-agent']in JavaScript.request.headers["User-Agent"]in Python.
Why a server cannot trust a request
On a paper order form, the customer can write anything in any box, so the person behind the counter checks every box.
The sender of a request writes every line, including the method and the path. Sam can change size=large in the address bar to size=free and then press Order. A program can also send a request with no browser and no person, and put quantity=-5 in the body. The code on the server has to check each value before using the value.
Check your understanding
Name the four parts of a request that hold input as named values.
Show the answer
The query parameters, the headers, the cookies and the body.
Which part of Sam's request does the JavaScript
req.body.quantityread?Show the answer
The body, which holds the two form fields.
Give one reason why a server cannot trust the quantity in a request.
Show the answer
The sender writes every line of a request, so the sender can write any value.