Differences
This shows you the differences between two versions of the page.
python_wiki:requests [2018/08/11 15:24] billdozor [GET] |
python_wiki:requests [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Requests ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | The requests library is a http library that allows you to interact with web servers via Python. | ||
- | |||
- | \\ | ||
- | Sites to Practice Using Requests Against: | ||
- | * Generic http request and response server: http:// | ||
- | * XKCD Comics: https:// | ||
- | |||
- | \\ | ||
- | **Checklist** | ||
- | * Python 2 or 3 | ||
- | |||
- | ---- | ||
- | |||
- | ====== Install ====== | ||
- | |||
- | Requests can be installed via pip | ||
- | <code bash> | ||
- | pip install requests | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== Import ====== | ||
- | |||
- | <code python> | ||
- | import requests | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== Using Requests ====== | ||
- | |||
- | Requests supports the following http request types. | ||
- | <code python> | ||
- | my_site = " | ||
- | |||
- | # GET | ||
- | response = requests.get(my_site + " | ||
- | |||
- | # POST | ||
- | response = requests.post(my_site + " | ||
- | |||
- | # PUT | ||
- | response = requests.put(my_site + " | ||
- | |||
- | # DELETE | ||
- | response = requests.delete(my_site + " | ||
- | |||
- | ##-- head and options responses are not implemented in the httpbin.org website --## | ||
- | # HEAD | ||
- | response = requests.head(my_site) | ||
- | |||
- | # OPTIONS | ||
- | response = requests.options(my_site) | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== GET ===== | ||
- | |||
- | Using the GET request and handling the response data. | ||
- | |||
- | \\ | ||
- | Example: Retrieve the latest XKCD Comic Info ( More Info: https:// | ||
- | <code python> | ||
- | # | ||
- | |||
- | import requests | ||
- | |||
- | # XKCD Current Comic JSON API | ||
- | my_site = " | ||
- | |||
- | # Retrieve using GET | ||
- | print(" | ||
- | response = requests.get(my_site) | ||
- | |||
- | # Extract the JSON object from the response | ||
- | data = response.json() | ||
- | |||
- | # Show http status code on GET | ||
- | print(" | ||
- | |||
- | # Show Certain fields from the JSON object | ||
- | print(" | ||
- | print(" | ||
- | print(" | ||
- | print(" | ||
- | print(" | ||
- | </ | ||
- | |||
- | \\ | ||
- | Output from above example | ||
- | <code bash> | ||
- | Sending GET to: https:// | ||
- | Status code is:200 | ||
- | |||
- | -- The Current XKCD Comic -- | ||
- | Comic Title: Pie Charts | ||
- | Comic Text: If you can't get your graphing tool to do the shading, just add some clip art of cosmologists discussing the unusual curvature of space in the area. | ||
- | Comic Date: 2018-8-10 | ||
- | Comic Image URL: https:// | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== POST ===== | ||
- | |||
- | Send data in a POST request. | ||
- | |||
- | \\ | ||
- | Example: Use the httpbin.org site to delay 5 seconds (will replace with a more interesting example later) | ||
- | <code bash> | ||
- | # | ||
- | |||
- | import requests | ||
- | |||
- | # Post the request (this will delay/sleep for 5 seconds) | ||
- | response = requests.post(" | ||
- | |||
- | # Extract the JSON object from the response | ||
- | data = response.json() | ||
- | |||
- | # Show http status code | ||
- | print(" | ||
- | |||
- | print(" | ||
- | print(data) | ||
- | </ | ||
- | |||
- | ---- | ||