python_wiki:requests

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python_wiki:requests [2018/08/09 23:37]
billdozor [POST]
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://httpbin.org/ 
-  * XKCD Comics: https://xkcd.com/json.html 
- 
-\\ 
-**Checklist** 
-  * Python 2 or 3 
- 
----- 
- 
-====== Install ====== 
- 
-Requests can be installed via pip 
-<code bash> 
-pip install requests 
-</code> 
- 
----- 
- 
-====== Import ====== 
- 
-<code python> 
-import requests 
-</code> 
- 
----- 
- 
-====== Using Requests ====== 
- 
-Requests supports the following http request types. 
-<code python> 
-my_site = "http://httpbin.org/" 
- 
-# GET 
-response = requests.get(my_site + "get") 
- 
-# POST 
-response = requests.post(my_site + "post") 
- 
-# PUT 
-response = requests.put(my_site + "put") 
- 
-# DELETE 
-response = requests.delete(my_site + "delete") 
- 
-##-- head and options responses are not implemented in the httpbin.org website --## 
-# HEAD 
-response = requests.head(my_site) 
- 
-# OPTIONS 
-response = requests.options(my_site) 
-</code> 
- 
----- 
- 
-===== GET ===== 
- 
-Using the GET request and handling the response data. 
- 
-\\ 
-Example: Retrieve the latest XKCD Comic Info ( More Info: https://xkcd.com/json.html) 
-<code python> 
-#!/usr/bin/python3                                                                          
- 
-import requests 
- 
-# XKCD Current Comic JSON API 
-my_site = "https://xkcd.com/info.0.json" 
- 
-# Retrieve using GET 
-print("Sending GET to: " + my_site) 
-response = requests.get(my_site) 
- 
-# Extract the JSON object from the response 
-data = response.json() 
- 
-# Show http status code on GET 
-print("Status code is:" + str(response.status_code)) 
- 
-# Show Certain fields from the JSON object 
-print("\n-- The Current XKCD Comic --") 
-print("Comic Title: " + data['title']) 
-print("Comic Text: " + data['alt']) 
-print("Comic Date: " + data['year'] + "-" + data['month'] + "-" + data['day']) 
-</code> 
- 
-\\ 
-Output from above example 
-<code bash> 
-Sending GET to: https://xkcd.com/info.0.json 
-Status code is:200 
- 
--- The Current XKCD Comic -- 
-Comic Title: Complex Numbers 
-Comic Text: I'm trying to prove that mathematics forms a meta-abelian group, which would finally confirm my suspicions that algebreic geometry and geometric algebra are the same thing. 
-Comic Date: 2018-8-3 
-</code> 
- 
----- 
- 
-===== 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> 
-#!/usr/bin/python 
- 
-import requests 
- 
-# Post the request (this will delay/sleep for 5 seconds) 
-response = requests.post("http://httpbin.org/delay/5") 
- 
-# Extract the JSON object from the response 
-data = response.json() 
-  
-# Show http status code 
-print("Status code is:" + str(response.status_code)) 
- 
-print("Returned response is:") 
-print(data) 
-</code> 
- 
----- 
  
  • python_wiki/requests.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)