Consuming HTTP Services in Python Transcripts
Chapter: HTTP services with Python builtins
Lecture: Concept: Python 3's urllib

Login or purchase this course to watch this video and the rest of the course contents.
0:01 How do we use Python 3's urllib set of submodules
0:05 to do basic get request against servers
0:08 and even more advanced ones like post, put and delete?
0:11 Well, if we are going to do a get request we simply create
0:14 a get request against api/blog and this is going to give us all the blogs back as a json body;
0:19 In code, we are going to need to import two modules,
0:23 urllib.request and json and then of course, we construct the url
0:27 that we are going to work with, and we are going to call url open,
0:30 just like before, but this time, it's urllib.request.open
0:34 and the response now supports working with context managers that is the with blocks,
0:41 so we don't have to be very careful about how and when we close the response,
0:45 so again, we are going to check the response code,
0:48 read the text and there is no json feature
0:50 so we are going to have to use the json module
0:53 to load that back into a Python dictionary,
0:56 after that, we're done with the service,
0:59 we are off to the races with plain old Python data.
1:02 So it gets pretty straightforward, but what about put or other modifying data operations,
1:07 so here we want to update a blog post, we want to do a put against api/blog/the id of the blog
1:13 and we are going to post as the body the changes we'd like to make to the post
1:17 and what we'll get back is confirmation details, so in code again,
1:21 urllib request and json url but now we are going to create a dictionary
1:25 that has all he data title, content and view count,
1:29 we are going to convert that to a json string via dump s and then,
1:33 we need to pass actual bytes not a string,
1:35 so we are going to give it the utf8 representation of that string as bytes,
1:41 okay, so that is the post data, let's go an throw in some headers,
1:45 so that we can say this is json you are going to be getting,
1:48 and now, this time, instead of actually just calling url open and passing some values,
1:52 we are going to create a urllib.request.request object give it the url,
1:57 the data, the headers, and now we can set in Python 3 the method to put,
2:00 so that is excellent, and then we carry on like before,
2:04 we issue the request, check the response code read the text,
2:08 and turn that back into a dictionary, so this is how you work
2:12 with urllib and Python 3's builtin http capabilities.