Exploring Requests and Responses
00:00 Exploring requests and responses. So you’ve made a request and received a response from the server, but what are those two things? A request contains all the data you send to the API, including the URL you want to send it to, the type of request you want to make.
00:17 In the previous example, you were making a GET request, but there are others, and headers, which you’ll learn about shortly.
00:25 A response contains all the data returned by the server. This includes the status code, the 200 you saw in your response, the actual data you requested, in our case, a list of emojis, as well as headers similar to the request.
00:41 So what are these headers? They contain data about the client sending the request, the server sending the response, the type of content being sent, like text, HTML, or JSON, what type of content the client can accept, and more.
00:55 They can vary, but common ones are Accept, which tells the server what types of content the client can accept, Content-Type, which describes the type of content being sent, for example, JSON or PNG, User-Agent contains details about the browser and operating system making the request, Server describes the software the server is using, and Authorization allows the request to carry passwords or API keys to gain access to private data.
01:23 You can add your own custom headers, which is helpful for including extra information in a request, such as an authentication ID. These are normally prefixed with an X.
01:34 Your response contains a status code, which tells you how well the request went. If your request was a success, you’ll have seen the 200 status code.
01:43 This means just that, the request was dealt with successfully. If you receive a 400 status code, that suggests something about your request was incorrect. 401 means you aren’t authorized to access that API.
01:55 Don’t worry, you’ll learn about authorization later. 404 is a status code you might have seen while browsing the internet. It means the link you were looking for couldn’t be found.
02:05 405 means that the action you were taking is not allowed. For example, trying to edit a read-only piece of data. And finally, 500 means something went wrong on the API’s end of things.
02:17
In the last example, you made a GET request using the requests.get function. This is an example of an HTTP method. These are a set of predefined methods of interacting with APIs, which allow you to do key tasks.
02:32 For example, instead of reading data, if you wanted to create a new object on the server, you could use a POST request.
02:40
Moving over to the REPL, you can explore your own request and response. Once again, you’re going to import requests and set url to the emojis API URL.
02:49
But this time, instead of immediately calling requests.get, you should store your response in a variable and continue to access it. You can access the original request using the response.request attribute.
03:02 Now you can see the parts of the request you looked at earlier. The URL,
03:13
You can even see it knows that you sent your request using the Python requests library. You can also explore the response. For example, you can access the status code and the headers.
03:27
GitHub has sent a lot of custom headers, but amongst them, you can find the Content-Type header. You can extract this header in particular using the .get() method, as headers is just a normal Python dictionary, which in the case of the emojis API should say application/json, which means the data the server has sent is in the JSON format.
03:51
You can access that data with the .text attribute, but to be even more helpful than that, requests can convert that JSON data into a Python dictionary to make it easier to interact with.
04:02
You can do that with the .json() function. Now you can interact with the emoji data like you would a normal Python dictionary. For example, if you want to find the link to the partying face emoji, you can.
04:15 Did you notice the API returned every single emoji available? What if you want to be more precise about what you’re asking for? Some APIs will let you filter and sort the returned data, and you’ll learn how to do this yourself using query parameters in the next lesson.
Become a Member to join the conversation.
