Quiz
- Can you explain again what 'body' means with the POST and PUT methods? And what's the difference for those two.
Here's the MDN page on POST, which has some examples.
Sure. The transmission from the browser to the server has a specific structure, namely a series of header lines in
key: valueformat, then a blank line and then the body. Like this:header1: value1 header2: value2 ... bodyHere's an example from today's demo:
POST /likes/apple HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate, br, zstd Accept-Language: en-US,en;q=0.9 Connection: keep-alive Content-Length: 0 Host: localhost:8080 Origin: http://localhost:8080 Referer: http://localhost:8080/ Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 sec-ch-ua: "Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "macOS" item=eggs&count=5In general, the body can be long or short. When uploading a file, it might be quite long:
------WebKitFormBoundaryvZIhbTFkpmvBlDx8 Content-Disposition: form-data; name="nm" 5377144 ------WebKitFormBoundaryvZIhbTFkpmvBlDx8 Content-Disposition: form-data; name="pic"; filename="Awkwafina.webp" Content-Type: image/webp UklGRioXBgBXRUJQVlA4WAoAAAAMAAAANwQAnwUAVlA4INoFBgAQpg6dASo4BKAFPjEWiUMiISEUusV4I AMEs7Dvu6b3h//nmD/D/+3PCxT8//+OT4+18VxY//3p2/eOnw1H/8PN58M78Zah3jQHgEW//zOuv6t/p+ Uj0T+4/8zx3/ov+54g3o3+R9gD+df2z/a+rn/kdw133/n+wB4UfMH9sen5/vvQL+1/YA8wv/F46P3//s+ ozDdPg/j1hs289H/Z/az8xfoN594vflj4RsQTk/NH65f93+c/0fty9mnjJfs570PXJzf3tj6Ij1/P4B0z F4+/Sf/Z5XfpX+X3n+bv6X/Df6P/y/573Ev9//MeBr2T+o/7f+b/1XsH/NfxR+7/v/+h/9n+X+d/+P/7/ 9n+XPpD+v/1f/q+8L5Bfzv+6f9D83f9H+9P1WfsfuT/z+/m3f/k//X/iewR7wfh/+1/oP3z/1fvZfXf9T /Pf6L8//l39P/yH/S/yX+i/bb////L9Af5X/UP9z/f/81/8/85////x9w/6b/6f6//b//n1HvvH+j/9/+ u/337x/YF/O/7P/4P8b/p/3L+lr+3/+/+6/4X7q+4P9M/0n/0/2P/B/P////or/O/79/5f8t9///7+/T/ /+nT/5ffD/dL//GS3vM1mm+b8ksKdpVkuaNkBhVLvrgAatP6SLcwCr8qi4/rZWnejrmDPXs1nRqDnwqJK aekOS5p3+/dxUwkUNwhXfwVOTmNS8kfsbu977Rkd9kf4Wrcjv9snc95YiR33A0+LDiDwE53S/vatow9U/ zRc0yi/SGghDZv6owi... ------WebKitFormBoundaryvZIhbTFkpmvBlDx8--
When POSTing an Ajax request, it might be quite short: just a bit of JSON, say
The point, though, is that for POST, the BODY contains the data, if any.
We can also look in the developer tools. I'll demo that when we get to the demo.
- And what's the difference between POST and PUT?
POST creates a new entity. Like SQL INSERT
PUT updates and existing entity. List SQL UPDATE
- How does JSON handle nested or complex data, like objects within lists within objects?
Quite well. Just nest the structures:
{place: {name: "Wellesley", address: {streetAddr: {name: "College St", number: 106}}}} - How do you handle user authentication/authorization with REST APIs?
Great question! You can certainly POST username/password values using Ajax. Or, you can use sessions.
Ajax requests include cookies, which includes the
sessioncookie, so you can do everything with Ajax that we do with non-Ajax. - None. I think the demo will solidify everything. / I think I understand just need some practise.
Let's do it!