RESTful and HTTP APIs in Pyramid Transcripts
Chapter: A nearly RESTful service
Lecture: Concept: RESTfully updating cars

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The most complicated method that we're going to work with
0:04 in our api is the ability to update a car.
0:08 So getting cars, pretty straightforward, creating cars somewhat involved,
0:13 but updating means we have to both get the car back
0:16 and make the changes to push it in there, so we have to be pretty careful here.
0:21 Now, the first thing that we're going to do
0:24 is we're going to use the request and we're going to go to the matchdict
0:27 and say give me the id that's in the url,
0:29 because we're doing a put of a json body to api /auto/ car_id
0:34 so we need to know what car you are talking about;
0:37 grab that there, use the repository to make a quick check
0:40 does this car actually exist, if it doesn't,
0:42 return 404, like hey there is no car with that id.
0:46 The next thing we have to do is parse the body,
0:48 and as you saw in that last example when I made the json malformed,
0:51 that can crash, how do we know a crash,
0:55 because we got the response 400 could not parse your json,
0:58 your body is json and that's in the except clause there,
1:01 so you want to make sure that that part goes into a try except
1:04 unless you're willing to take 500 when they send you a bad request,
1:07 which is okay, but it would be better to communicate like
1:11 the problem is on your end, not ours, so please, don't bother us.
1:14 Then, we have to actually do the work,
1:16 we're going to go update the car, and we want to return something,
1:19 so in this case, there is nothing really new about the car,
1:23 other than what they have already told us,
1:25 there is no reason to send additional data back to them
1:28 and say and here is the updated version, because guess what,
1:30 they gave us all the updates.
1:32 So, what we decided to do is return a 204 status code which means
1:35 successfully updated but no content.
1:37 So, that worked out really well,
1:39 in the case of the post, we decided to use 201 created
1:42 which is the best response for the post operation.