The GET Request
This lesson covers the HTTP GET request in general and how to create one using the requests
library in specific. You’ll also get a short introduction to response status codes.
00:00
The GET
request indicates that you’re trying to retrieve data from a specified resource. We’re going to be testing it out with GitHub’s Root Rest API, with the URL api.github.com
. It’ll return a response, which is basically an object containing the results of your request.
00:16
Let’s take a look at that. After importing requests
, let’s save the URL we’re going to go to as a variable.
00:25
We’re going to be going to this URL several times throughout the tutorial, so it’ll make it a little bit easier to perform a request. Let’s try it out. It’s simply requests.get()
and then we’d put the url
or whatever URL you have that you want to try out. Congratulations!
00:42
You got a Response
with a status code of 200
. The information that comes back from your GET
request can be saved into a Response
. This Response
object, if we create a variable for it, we can do a variety of methods against it that allow us to inspect it and look at the contents. First off, we can just simply enter the name of the variable back in, and we can see that it shows that same Response
and status code of 200
.
01:09
So let’s talk about status codes a little bit. That’s one of the first things that we can look up as methods, .status_code
coming back being 200
.
01:18
You’ve seen other status codes before—you might’ve seen one for, let’s say, like a bad URL. Going to 'api.github.com/'
—we’ll put a little side note here that we’re going somewhere invalid.
01:34 Let’s try our response this time… and we’ll use the invalid one.
01:43
So, this time it shows a 404
. So what is a status code 404
? You’ve probably seen that before. It means that the resource is not found.
01:53 Let’s talk a little more about status codes just briefly.
Chris Bailey RP Team on April 23, 2019
Hi Kane. Dan Bader made a tutorial about using bpython on his site dbader.org Here is a link. And here is a link to the bpython homepage where it shows installing using Pip.
apyle0710 on April 27, 2019
Hello,
I keep the getting the following error. Have Google dozens of times looking for a fix, but can’t find it. Would greatly appreciate any help!
(base) Andrew-XXXXXXX-MacBook-Air:Desktop andrewXXXXXX$ python3 scraper1.py Traceback (most recent call last):
File “scraper1.py”, line 1, in <module> import requests File “/Users/andrewXXXXXXXXXX/Desktop/requests.py”, line 4, in <module> requests.get(url) AttributeError: module ‘requests’ has no attribute ‘get’
Dan Bader RP Team on April 28, 2019
@apyle0710: Looks like you have a file named requests.py
in your app folder. And that’s the requests.py
file that Python imports when you do the import requests
. So the problem is that your local file shadows (overrides) the requests
library you’re trying to import. The easiest solution is to rename your local file to something else. Hope this helps :)
Harsh Chaklasiya on May 7, 2020
import requests url = ‘aspirebit.com‘
requests.get(url)
Respose
/Users/harsh/PycharmProjects/test/venv/bin/python /Users/harsh/PycharmProjects/test/venv/test.py
Process finished with exit code 0
BLANK with NO ERROR!
Running in PyCharm MacOS Catalina 10.15.4
Harsh Chaklasiya on May 7, 2020
Ricky White RP Team on May 7, 2020
Hi Harsh Chaklasiya.
Make sure you are using print()
if want to see the response in the terminal. For example:
import requests
url = "https://aspirebit.com" # Don't forget the HTTP / HTTPS
res = requests.get(url)
print(res)
This will print the request response <Response [200]>
.
Hope this helps.
Become a Member to join the conversation.
Kane wadel on April 23, 2019
please do you have link for install bpython on windows to use the autocompletion on the visual studio code terminal