Now in a previous skill when we first talked about REST APIs, I happen to
highlight a very
popular utility for interacting with REST APIs known as Postman. This is really
good because
you can ultimately save your endpoints, you can easily browse them. It really
is very,
very handy for testing purposes, but that said, if you want to interact with a
REST API and
receive the data, like JSON data, and then work with it in a programmatic
manner, utilizing Python,
then what we would like to have would be a dedicated Python library that is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
then what we would like to have would be a dedicated Python library that is
effectively going to
allow us to talk HTTP. Well, the good news is that there is such a library and
this one is called
the Request Library. So let's walk through the basic installation of Request
and then we'll do
some testing. So very similar to what I did when I happened to install NetMeko,
what I will do
is I will create a little virtual environment here. This is going to allow me
some segmentation
or isolation should I say. So now I've created it, let me just activate it. And
now we can see here
in the corner my virtual environment has been activated. So this means anything WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in the corner my virtual environment has been activated. So this means anything
I install is not going to
be installed system-wide instead it's going to be installed with fitness
environment only. So I
will now say it's pit 3 install request and to answer give a moment and as we
can see here,
I have successfully installed a request library. So let me just create a little
test folder, just
called test, keep it nice and simple. And let's go inside it. And what I will
do is I will create a
little file called script1.py. So remember we were using Postman, but we were
using Postman to send
requests to this little test-reached API. So let's click on this here once WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
requests to this little test-reached API. So let's click on this here once
again. So what we can see
here is we have our endpoints such as getting information on the actual users.
So what I'm going to do
here is I'm going to take this information but as opposed to connecting to the
rest API over Postman,
I'm going to use the request library to do the same thing. So the first thing I
will do is I will
grab this URL right here. So I've got a copy of it. And let me go in and open
up this script here.
All right, so first thing is first what I'm going to do is to import the
request library. And what
I will now do is I will create a little dictionary. Let me just pull this down WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I will now do is I will create a little dictionary. Let me just pull this down
a little touch. And I'll
call the dictionary headers okay. And this is going to allow me to define the
headers that I want.
So in this case here, I'm going to have an accept header and the corresponding
value is going to be
application forward slash JSON. And remember, if you recall, this is just
similar to what we did when
we made a request via Postman. Remember, I can make my get request paste in the
URL that I want to
target. And in my headers, what I did was specify an accept header. And one of
the valid values is
like I say, it's application forward slash JSON right here. So if I just happen WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
like I say, it's application forward slash JSON right here. So if I just happen
to send this off,
as we can see, we get the response, let's do the same thing with our Python
code. So we'll
define our headers. And now what I will do is I will create a little variable
to store my result. So
I'll just say response is equal to, and then I'm going to take the request
library that I'm just
importing. And then I'm going to say dot get this will be the get method to
actually send a get
request. So what I also want to do is I want to define the URL that I want to
target. So I'll just say WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
target. So I'll just say
URL is equal to and paste in this particular value right here. And all I'll do
is also the URL
is equal to the URL. I've just defined above. I'll say the headers that I want
are equal to the
headers library or library. The header dictionary I've just defined above
clearly I'm not getting
enough sleep these days. Now what I'm also going to say is verify is equal to
false pretty much.
I'm not looking to verify my SSL certificates or my TLS certificates. Now check
this out. What we've
done here is we've taken the library going to send a HTTP get request, but WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
done here is we've taken the library going to send a HTTP get request, but
specify the URL and the
headers and we're going to save the response and this particular variable here.
So what this
therefore means is if I just print out the response, which I actually see some
pretty interesting
information. So let's just save this. And if I just go and attempt to run my
little script or
I'll say Python 3 script 1.py and to enter, look at what we actually see here
response 200. This
is as seeing the actual status code. And what I'm also getting here is this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is as seeing the actual status code. And what I'm also getting here is this
particular warning.
So in order just to suppress that warning. So it's not going to be quite so
annoying. I'm just
going to disable it by saying requests dot packages URL lib3 and I'll say dot
disable warnings.
So let's try it again. Arrow up, hit enter. Now we're just seeing the response,
but this is not
actually showing us the data we got back just mainly the response code. However
, this particular
variable is going to have some particular attributes associated with it. So
what I can say here is show
me the text attribute, which ultimately shows me the content of the response in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
me the text attribute, which ultimately shows me the content of the response in
Unicode as we see
right here. So what this means then is if I actually run this one more time,
now we're actually seeing
all of the data that's coming back here are all of the names, the emails, the
gender, the status,
so on, so forth. Now as it stands right now, this type of information is just
regular string data.
Okay, so it's not programmatic, even though it does look like it. So if I just
print the type and
hit enter, indeed, we can see this is just a regular Python string. So we can't
go and work with a
list or call any of the dictionary case, but what we can do is we also have an WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
list or call any of the dictionary case, but what we can do is we also have an
additional attribute
here. So I just happened to print this out and I just say print the response
dot json and then I
use my parentheses to actually call the JSON object. If I run the script, the
information looks the same,
but this time, if I actually check the type one more time, look at this right
here, the outer object
is actually a Python list as the note of via the square brackets. So what this
means is we can now
access the data programmatically so I can grab list index position zero, run
this, enter,
this now gives us the first item in the list, which is this right here. And now WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this now gives us the first item in the list, which is this right here. And now
we can grab the
particular keys. Let's maybe say want to get the email. Let's go and grab this
to call the email key.
And now we're able to program it to access the very first email within all of
this data.
So unlike what we were doing with postman, we're not just retrieving the actual
information back
and testing the response now with Python, but able to access this data as
Python objects and
begin manipulating it programmatically. This is very, very important when
working with networking
information, particularly when you want to conduct network tests. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
information, particularly when you want to conduct network tests.