Consuming HTTP Services in Python Transcripts
Chapter: Consuming RESTful HTTP services
Lecture: Concept: updating a post with requests

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In our service, to update a blog post,
0:04 we are going to do an http put to the particular url
0:07 that represents that blog post, and the body of that post is going to contain
0:12 the details in json form to replace the value on the server,
0:15 so we want to do a put/api/blog/id and pass the title and the content, things like that.
0:21 The response is going to be just an http status code 204, no content,
0:25 everything worked, thank you.
0:28 In code, what does that look like?
0:30 Well, we start by creating a dictionary that is going to be the body of our post,
0:33 so updated post details, we are going to pass title, content, view count and published.
0:37 Now, not all of these necessarily changed, so it's up to you
0:41 and really up to the API to determine whether you have to pass all the values back
0:46 or just the ones that are changing, I believe the API we are working here
0:49 requires all the new values to be passed or it will like null them out on the server.
0:54 So, we are going to pass them all but it could be possible if you just want to change the title
0:58 you might just have to pass the title, that is up to the API.
1:01 Then we come up with the post url that we are going to update,
1:05 API/blog the id of that post, and then what we do is we do a request.put,
1:10 so we send a put request, or http request with a put verb to that url
1:14 and we pass the json body as the updated post details,
1:18 remember, that is going to take the dictionary and convert it to a json string
1:21 and set the content type of the put to be application/json.
1:27 And then of course we want to check the status code, and make sure that worked,
1:31 in our case, the contract we have with the servers 204 is good,
1:35 everything else not good, but you might want to be on the lookout for 200 as well
1:38 if they not carefully managing their status codes in the response,
1:42 and if that worked, then, yeey, we're all good, now we can just assume
1:45 that that post was updated as we specified.