Consuming HTTP Services in Python Transcripts
Chapter: HTTP services with Python builtins
Lecture: Python 2: Blog explorer update a post with urllib2

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Doing a get to get the post from the API was actually pretty straight forward, so let's see how this works for adding a post.
0:09 We're going to use a lot of the same structure that we had before, we are going to get the same user interaction, we are going to get the same data,
0:16 this time we do need to explicitly set the content type because it's not going to do that for us,
0:22 so then we're going to come down here and we would like to do something like this,
0:25 urllib2.urlopen() and we can pass in data, but we can't like pass our headers for example,
0:32 so in order to do that, we have to create a request type object and I am going to call it req so you don't confuse it with the request,
0:39 the other external package, so we'll create urllib2.request object, and in here we can pass the url, the data, the headers and so on.
0:50 So we'll say url and we'll say data= and I am going to hold off for a second there
0:54 we'll say headers=headers, so this is not going to jsonify this for us, so we have to say json.dump s of our post data dictionary, like so.
1:04 Okay, so then we're going to do that, we're going to post this down here, we'll do a few things down here, say we want to response.close
1:11 because remember we don't want to forget that, and down here we want to do response.close actually let's do it right here,
1:17 we could do a try, finally, but we are just going to avoid that for a second, so this should have a p in it, excellent, okay, so this is going to work
1:24 and it's going to do a post request, how do I know, I am just opening it, well, check this out, down here, we can get the method, if it has data,
1:34 it's a post, if it doesn't have data, it's a get. Okay, so the fact that we add this data means we're doing a post request,
1:42 we can do a get request with data, and we can do a post request only with it,
1:48 so we can't do a post request without data, I guess that doesn't make any sense, but it's pretty constraining, as we'll see here in just a moment,
1:55 okay, so we can't do the status code, afterwards, this is more or less the same, we'll say response.get code and we'll say read like so,
2:05 okay so if it doesn't work we're going to get that, and remember, we have to close it straight away, and then here let's say,
2:12 or we'll better do this before text=response.read and then here we can say json.load from string the text and that will be our post
2:23 and hopefully it worked, let's go and run it, let's do a list, alright good, let's add a new post, this post comes from Python 2,
2:33 Python 2, and the view count is going to be one, how it's going to work, here we go, boom, created, alright, beautiful, let's list it again,
2:43 alright, now this post comes from Python 2, okay so that worked, let's just really quickly look at the two other options, update and delete.
2:52 They are not going to go so well, well, update says in order to update a post
2:57 we have to do a put to url, well, with urllib2 we can only do a get or a post, so okay call that function, can you, delete similarly,
3:06 we have to pass the delete verb, we can do a get or a post it has data or doesn't have data, too bad, you won't be able to call that either,
3:13 well, there is some really string reasons to work with requests from Python 2, right there, huh?


Talk Python's Mastodon Michael Kennedy's Mastodon