Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Request Headers

Give Feedback

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.

00:00 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.

00:12 You could limit it to certain media or certain types of information that you want returned. Another type of header would be for Authorization, which we’re going to discuss in Section 3.

00:21 So, let’s try this out with Accept. Let’s modify our search script that we wrote earlier. We’re just going to change it and add another section to it.

00:32 Let’s make some space. So, we’re going to add headers, the dictionary for it. In this case, we’re going to say what we will accept.

00:47 GitHub has a text-match media type that we can accept, so let’s enter that in.

00:55 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.

01:28 So again, it’s going to return "text_matches", so we’ll look for that key and we’ll have it print them out. Let’s save this and give it a shot. Oh.

01:39 We’re going to change, actually, what we’re searching for. We’re going to go back to what we were searching for before, which was for 'requests', so it’s going to look for the word requests.

01:52 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 'Repository'. '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 12 to 20, so if you were to count over 12 characters and go up to 20.

02:31 So, what that would allow you to do is you could highlight where—within this 'description'—the word 'Requests' appears. So we could do something similar, we could—I don’t know—search for maybe the term 'HTTP for Humans'.

02:48 And we’ll try it again.

02:52 So in that case, the 'text' 'HTTP' is from 7 to 11—being right here. Actually, it broke out the phrase 'for Humans' and that came up for the index of 21 to 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.

03:16 All right. We’re going to talk a little bit further about other HTTP methods.

mikesult on May 5, 2020

As DaveyD and I noticed. search.py now returns a different repo with different info from the video …’fragment’: ‘Requests + Gevent = <3’,

My guess is a new repo at github has changed what is at index 0 of the json.items. I’m still don’t quite understand why it gets to be in the front of the line.

Become a Member to join the conversation.