From the course: Python: XML, JSON, and the Web

Retrieve and send data - Python Tutorial

From the course: Python: XML, JSON, and the Web

Start my 1-month free trial

Retrieve and send data

- [Narrator] Let's use what we've learned so far to try out the requests library. And again, we're going to be using the httpbin.org site to test our code the same way we did earlier in the course. So here in VS Code I'll open the requests_start.py file in my requests chapter. And you can see I currently have an empty main function along with a function named printResults which will print the returned data from the server to the output. So let's look at that function first. The first part prints out the result code returned by the server and it does that by looking at the status code property of the result object that's passed in as part of the function parameter list here. Then it prints out the headers, and then it prints out the returned content. And it uses the content property for that which we'll come back to later. Alright, so let's write our code to make the request. So I've already imported the requests library up here at the top and what I'm going to do is first declare the URL to use which is going to be the httpbin end point that returns the sample XML code which is /xml. Then I have to write the code to make the actual request and that's very simple. I'll just simply say result equals requests.get and then I paste the URL and then we'll call printResults to print out the result of that operation. Alright, so let's go ahead and run what we have and remember, if you've got Visual Studio Code you can just go to the debug tab and run this. If you don't, that's okay. You can just go to the terminal prompt and run this directly using Python. So I'm going to run this here in VS Code. And after a few moments, you can see that the results come back. So here up at the top of the output is the status code of 200 indicating that things are okay. And that's followed by the headers right here. And then that's followed by the response content. And again, notice the response content is written here as a series of bytes. Right, there's that little B character right here. And that's because we're using the content property of the response object. However, we can use the text property instead to print out the content that has been decoded for us as text. Now normally, you would check to make sure that the response content was in fact text by looking at the encoding property. But for the purposes of this demo, we know that the response is going to be text. So I'll just change the property in the printResults function from content to just be text. Alright, and if I run this again, now you can see that the response is nicely formatted XML data because it's being properly decoded as UTFA text. And the reason for this is that the requests library automatically tries to decode the returned data for you. So now let's try sending some parameters along with the request. And we'll see how simple that is. So for this, I'll use a different URL which is the get endpoint. And this endpoint just echoes back the content of the get request in JSON format. And so, I'll define a dictionary of some key value pairs to send along with the request and I'll call that dataValues. And I'll just give it some keys and values to use. Alright, and then finally, I have to issue the request and then print the results. So I'll just copy these two lines, paste them here And to send the data, I have to use the params argument and set that to be equal to the data values dictionary that I created right here. And notice I don't have to do anything special like I did with URLlib. I don't have to encode it or do anything. I just use the dictionary as it is. So let's go ahead and save and let's run this and let me, let me comment down this line so we only get some limited output. Alright, and so here are the results. You can see that in the returned data in the JSON object, there's an args section and the args section contains the keys and values that were passed to the server. Alright, let's see how easy it is to use the post-operation. So let me clear the console and go back. Now I'll use the same data values dictionary I've already defined and I'll just simply use a different URL. In this case, I'll use the post endpoint and that means I have to change this to be post. So remember, requests has method names that map directly to the http verbs. And instead of the params argument, I need to use the data argument and set that to be my dictionary. So I'll change this to be data and I'll just leave everything else the same. So let's go ahead and run this. And once again, you can see that the result code there is 200 so everything is fine. And this time, the data returned has my arguments here in the form section. So rather than in args because they weren't passed as part of the query arguments, it was passed as part of the encoded form data as part of the post request. Okay, let's do one last example. And this time, instead of parameters, we'll send a custom header to the server instead. So I can just use the previous code for this and what I'm going to do is copy this and paste it down here. And this time I'm going to use get and I won't use any data values. What I'll do is I'll say header values equals and I'll just simply say that the user agent is the Joe Marini App version 1.0.0. I'm just making things up here. Alright, so we'll save that. And then I need to change the argument from the data to headers and instead of using data values, I'll use header values and I'm going to use the get method instead. And then I print out the results. So let's run this. Oh, actually before I run this, let's comment that out. Okay, so let's save and let's run. Alright, and if you look in the results here in the returned data, so first the result code is 200 so that's good news. And then here you can see that the user agent of Joe Marini App 1.0.0 was sent as a custom header to the server. So this should start to give you some idea of how much simpler it is to use requests instead of URLlib and in fact, the requests library is overwhelmingly preferred by Python developers of all stripes.

Contents