Consuming HTTP Services in Python Transcripts
Chapter: Consuming RESTful HTTP services
Lecture: The HTTP verb meanings

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Let's look at these http verbs in a couple of ways, here we are going to play around with just a generic example and then later,
0:09 we are going to go look at a public very thought out API. So imagine we have some service running that someservice.com/api
0:17 and it has a certain number of resources and its specifications, so users and then users/ some kind of id based on what you are looking for
0:26 and maybe even /id/picture to get their picture. also projects and project details, so project 11 and even files about them,
0:33 so we might issue a get request against projects and that would return let's just say there is always going to be json
0:39 and give us a json representation of maybe the id and title of every project that you have access to. Then maybe in there somewhere is project 11
0:49 and that is the one you want details at so you could do a get against project 11. Now, maybe you don't have any projects, you want to create one,
0:56 well typically the way these http services would do that is we would do an http post against /project
1:05 and we'll see that that would tell the service hey I would like to create a project
1:09 and the body of that post, the data in that post would be the required details
1:13 to create a service, maybe not everything about it, but things like its name, who the owners are, stuff like that.
1:18 Similarly, if we wanted to update a user, we might go to /users, we know we want to update user 42, we need to do an http put against that,
1:27 so you see we combine the verbs with the resources to get all the behaviors that you are looking for, not all services work this way,
1:35 put and delete is becoming increasingly popular people use these, but get and post those are the mains of http services.
1:43 So, how do I know that doing a put against users/42 is supposed to update it
1:50 and doing a post against users should create a new user in the first place. Well, if we are following these http services
1:58 and we are trying to use these verbs to have these meanings, we just have to look at the actual definition of get, post, put and delete.
2:04 So if we go over to the Wikipedia entry for the hypertext protocol we'll see we have four verbs that I pulled out here
2:13 there are actually many more patch and whatnot that could be used but these are the real main players in these services,
2:19 and notice, only get is what is called item potent. Item potent means that if you apply it one time, something may happen,
2:26 if you apply it five more times, it will be exactly as if you've only applied it once,
2:31 so basically it means you can apply it to itself as many times as you want, whereas post, put and delete applying them a second time
2:39 might actually have a separate meaning, okay. So if we look at get, it should only retrieve the data and basically have no other effect,
2:47 if we look at post, this should tell the server to accept the entity enclosed in the body,
2:52 and create as a subordinate, so if I want to create a user with id 42, I would do a post of that user to /users and then we theoretically have
3:01 /user/42 was created as part of that action whereas put is supposed to replace or update the entity at that location,
3:10 so do a put to /user/42 should update user 42. And finally, doing a delete against user/42 well, you can bet that user is gone.


Talk Python's Mastodon Michael Kennedy's Mastodon