-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests_quickstart.py
53 lines (43 loc) · 1.38 KB
/
requests_quickstart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
# @Author: Pan Chao
# @Date: 2017-09-03 17:17:33
# @Last Modified by: Pan Chao
# @Last Modified time: 2017-09-03 17:25:10
'''
Reference:
- http://docs.python-requests.org/en/master/user/quickstart/
'''
import requests
# Make a Request
r = requests.get('https://api.github.com/events')
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')
# Passing Parameters In URLs
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)
print(r.url)
# Get Response Content
r = requests.get('https://api.github.com/events')
r.text
r.encoding
r.encoding = 'ISO-8859-1'
# Parse JSON Response Content
r = requests.get('https://api.github.com/events')
r.json()
# Custom HTTP headers
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
# Access and Send Cookies
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name']
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
r.text
# Timeouts
requests.get('http://github.com', timeout=0.001)