Customizing requests with your library.
Hello, everyone.
So far, we learned how to obtain headers of certain files, but why we actually need them and what
is their use?
Well, usually it is a very good practice to add headers to the request even before sending it.
So if I create a new here to try to do this, we basically need to follow three steps in.
The first step will be create request object and this is usually done by the request.
Class and we actually already use that class, then we would like to add headers to the request object
that we create from the request class.
OK.
And after we create the request, object puts a physical thing.
So from the blueprint of objects of requests, sorry, you create object call to request.
So after you created it, you would like to open it.
So we would use.
And often feature.
In fighting to open the request object and actually this feature you're also familiar with, and this
is the Eurorail open feature.
OK, you can use this from the Python packages.
So let me show you how this is done.
If I go to the terminal and now we're in python mode, let's first import some Labruzzo right from Eurorail.
Library dot request import.
Request Colma Eurorail open so you can import both of them at the same time and then following step
two here sort of step one after we imported it, we need to create an object from the request glass.
That's very simple.
Let's write the request equals.
Request.
So now we're creating an object from the request clause and the.
All project is going to be referred to the HTP
YouTube dot com.
OK.
That them and then if you'd like to other hadrosaur, for example, now the request is created, but
it's a good idea as we head toward the header to the request and this is the second step.
And how do we have here?
Very simple.
Let's try to request the dot out here and here.
You can basically tab the header that you would like to add.
For example, I can see a separate language actually do a dash here.
A language.
OK.
Cómo.
And now or let's do em for English, OK, let's hit enter here and for example.
Let's now open our request and see the hear of the request.
So obviously when you want to check out a request, you sometimes will see all the information because
there is plenty there.
But maybe you would just like to see some information that will do what the data there is all about.
So you can simply do a response because you're so open.
And then I will do request.
I worked in the records and then we can actually read the lines from that, the request that we already
made.
So here we already open the request, which is a third step with the euro open.
And now let's check actually the result of our work.
So if I do response that read lines and then I would like to check, let's say the fifth line.
So fifth, OK.
And you can see how massive for example, the fifth line it is.
I mean, I see it missed here, but if you have a software to open this line in a structured way, it
won't look that messy.
So if I would like to check to see the header items, I can do Rikyu dot ared sorry.
Raicu dot header items.
OK.
And if I hit enter, you can see that the code here is the YouTube dot com.
The user agent is Python three point nine.
And then you also get the addition to the header, that violent language m.
OK, so this is how you can actually act.
Information to the header is quite important, especially if you want to add something for the readers
or for the people that are checking your request.
So as I said, you can simply use these three steps, creating the request from the request class and
then adding the header, then sending the request by the euro command.
And finally, you can simply check the header by this comma and you can see that the additional here
that you added is already there.
So let's see how this will work if we added in the file.
So let's create a new python file called add header.
OK, so let me get a bit closer and open this up a little bit so we can see actually better.
OK, I think that's better.
So let's create the file.
Now let's do a hashtag user being EMV.
So this is where Python is vital three.
OK, and then we can do from bureau library not to request import requests and then we can do user agent.
OK, this is a custom variable and we can set the user agent as.
Mosiuoa.
5.0, this is the version, then Windows.
Ninety five point one same McAloon RV, and then we can do to any point zero that said, the lowest
rate, the G, C, K, all twenty, ten to zero for one or one.
And this is simply an example, user agent and then Firefox.
Slashed 20.
OK, and then the URL, so I'll write you your URL will be equal to H TTP.
YouTube and dot com, our favorite browser, and after that, I will write a function.
So let's try find out.
Heather, and this will be our main function.
Let me underscore here, that's it.
OK, so let's create the variable.
This will be called Heather's Equals Said.
Language.
In the same column.
And one.
Comma, and then I will do user.
Agents in the same column, user agent that already created.
OK, so actually, the user agent is going to be just one of the predefined variable that have already
created up here.
Let's go to the next one.
So I to request.
Equals to request and then Eurail.
OK, so this is the area, that group predefined YouTube dot com.
Actually here I missed the slushies, but that's fine now.
So let's go after that.
Heather's equals.
Heather's OK, and then this right to print and in the print statement, I will write request headers.
OK, and then for Key to follow in the request to adopt Heather.
Items.
OK.
So for this, I will print.
And here we're going to print percent is.
That percent is then percent key value.
Let me right, come on here.
You follow, and that's it, actually labor removed and I wouldn't put it exactly here.
That's it.
So first of all, the key and then the volume, OK, and now let's run the function by simply typing
if name.
Equals Main.
Look, month underscores the first name, of course, main, then I would add.
Heather, OK, to run the function, this how best to run the functions in Python, so let's save this
file now and let's try to run it from the terminal.
So how is it the python mode?
And let's try.
It was here to see what we've got.
So let's write Python Python up, Heather.
And you can see that after a press of Heather, we automatically get the same language as anyone as
we set it up.
OK, the user agent, we get the user agent to set it up on the top.
And so this was the information that we requested in here in the header section.
So our test is completely successful in the final run properly.
So this is how, guys, you can pretty much design and art headers to your euros.
I hope this video was useful and I see you in the next one.