In this lesson you’ll have a closer look at HTTP headers. You’ll learn how to set them using
requests and have a closer look at the Accept-header.
Let’s talk about request headers. You can further customize your request. They allow you to pass a dictionary into your
GET request. One example is
Accept. With that, you can specify what is acceptable for the response.
We’re going to also say that it needs to be JSON. I’ll add some notes here. So what this is going to do is it’s going to search through the response and return an array that will tell us where that search term appears—at least, the first appearance. We’ll use the same
json_response, and from the
repository again, we’re going to just grab the first hit, the first item. We are going to change this a little bit.
All right, let’s get that top hit. Save… and let’s run it. Okay. Let’s read through this a little bit. So, what did it return? Inside of here, it shows us the repository and the
'object_type' is a
'property' being—it’s coming from the
'description'. And then the
'fragment' of the
'description', here it says the same that we looked at before—
'Python HTTP Requests for Humans™'—and then the match did the text-match for the word
'Requests' and the index is from
20, so if you were to count over 12 characters and go up to 20.
So, what that would allow you to do is you could highlight where—within this
'Requests' appears. So we could do something similar, we could—I don’t know—search for maybe the term
'HTTP for Humans'.
So in that case, the
'HTTP' is from
11—being right here. Actually, it broke out the phrase
'for Humans' and that came up for the index of
31. So, just showing you another way that you could be using, in this case, headers and the
Accept header to do a little bit more of a special request.
Become a Member to join the conversation.