forked from WilliamQLiu/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_requests.py
More file actions
304 lines (255 loc) · 10.3 KB
/
example_requests.py
File metadata and controls
304 lines (255 loc) · 10.3 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
The internet is basically made up of requests and responses.
Using Python's requests library, we can look into how this works.
http://docs.python-requests.org/en/latest/api/
Commands
GET
Read an existing resource. This is like SELECT in SQL
HEAD
Similar to GET except server doesn't return a message-body in
response. Instead, it gets the metadata of an existing resource.
POST
Creates a new resource. This is like INSERT in SQL
PUT
Updates an existing resource. This is like UPDATE in SQL
PATCH
Usually not implemented. Updates part of an existing resource.
This is like UPDATE in SQL
DELETE
Deletes an existing resource. This is like DELETE in SQL
Important HTTP Status Codes
200 OK means Success
GET - returns resource
PUT - Provides status message or returns message
201 Created means Success
POST - Provides status message or returns newly created resource
204 No Content means Success
Completed, but nothing to return (because of no content)
304 Unchanged means Redirect
There's no changes since the last request (usually used to checking a field like 'Last-Modified' and 'Etag' headers, which is a mechanism for web cache validation)
400 Bad Request means Failure
PUT - returns error message, including form validation errors
POST - returns error message, including form validation errors
401 Unauthorized means Failure
Authentication required but user did not provide credentials
403 Forbidden means Failure
User attempted to access restricted content
404 Not Found means Failure
Resource was not found
405 Method Not Allowed means Failure
An invalid HTTP method was attempted
410 Gone means Failure
A method was attempted that is no longer supported.
E.g. mobile apps can test for this condition and if it occurs,
tell the user to upgrade
500 Internal Server Error means Failure
The server encountered an unexpected condition
Sample API URLS
api/v1/resume
for GET and POST
api/v1/resume/:slug/
for GET, PUT, DELETE
api/v1/job
for GET and POST
api/v1/job/:slug/
for GET, PUT, DELETE
Same goes for say api/v1/education and api/v1/experience
slug represents a variable (e.g. the resume id)
"""
import json
import requests
def get_webpage_details(site):
""" GET details of a request """
r = requests.get(site)
# Status Code
print "GET Response Status Code: ", r.status_code # 200
print r.headers # Gets all headers as a dict
"""
{
'content-encoding': 'gzip',
'transfer-encoding': 'chunked',
'connection': 'close',
'server': 'nginx/1.0.4',
'x-runtime': '148ms',
'etag': '"e1ca502697e5c9317743dc078f67693f"',
'content-type': 'application/json'
}
"""
print "Get specific field (e.g. 'content-type'):", \
r.headers['content-type'] # Get specific field
# application/json; charset=utf-8
print "Get encoding: ", r.encoding #utf-8
#print "Get Text: ", r.text # Get all text of page
#print "Get JSON: ", r.json() # Get everything as a JSON file
def request_API_calls():
""" Using all HTTP request types (POST, PUT, DELETE, HEAD, OPTIONS) """
r = requests.post('http://httpbin.org/post') # Example of POST
print "POST: ", r # <Response [200]>
r = requests.put('http://httpbin.org/put') # Example of PUT
print "PUT: ", r # <Response [200]>
r = requests.delete('http://httpbin.org/delete') # Example of DELETE
print "DELETE: ", r # <Response [200]>
r = requests.head('http://httpbin.org/get') # Example of HEAD
print "HEAD: ", r # <Response [200]>
r = requests.options('http://httpbin.org/get') # Example of OPTIONS
print "OPTIONS: ", r # <Response [200]>
def pass_params_in_urls():
"""
How to pass data in the URL's query string
By hand, getting URL would be given as key/value pairs in the URL
after the question mark (e.g. httpbin.org/get?key=val), but instead
we have a 'params' that we can pass a dict into
"""
# If you want to pass 'key1=value1' and 'key2=value2' to 'httpbin.org/get'
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
# Again, this is the same as http://httpbin.org/get?key2=value2&key1=value1
# Verify that URL has been encoded correctly by printing out URL
print "URL is: ", r.url # http://httpbin.org/get?key2=value2&key1=value1
def post_form_data_request():
"""
If you want to send form-encoded data (like an HTML form), then
pass a dictionary to the 'data' argument; the dict will be auto form
encoded when the request is made
"""
url = "http://httpbin.org/post"
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=payload)
print r.text # see how data goes into 'form'
"""
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.3 CPython/2.7.9 Darwin/14.1.0"
},
"json": null,
"origin": "74.71.230.126",
"url": "http://httpbin.org/post"
}
"""
# If you want to send data that is not form-encoded, pass in a string
payload = 'This is a test'
r = requests.post(url, data=payload)
print r.text # see how it goes to 'data' instead of 'form'
"""
{
"args": {},
"data": "This is a test",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "14",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.3 CPython/2.7.9 Darwin/14.1.0"
},
"json": null,
"origin": "74.71.230.126",
"url": "http://httpbin.org/post"
}
"""
def pass_headers_in_request():
"""
Add HTTP headers to a request by adding a dict to the 'headers' param
"""
url = 'https://api.github.com/some/endpoint'
payload = { 'some': 'data' }
headers = { 'content-type': 'application/json' }
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r
def response_content():
""" We can read the server's response """
r = requests.get('https://developer.github.com/v3/activity/events/#list-public-events')
print "Server's Response is: ", r.text
# When you make a request, Requests makes an educated guess on encoding
# based on the response of the HTTP headers
print "Guessed encoding is: ", r.encoding # utf-8
#print "Peak at content if unsure of encoding, sometimes specified in here ", r.content
def json_response_content():
""" There's a builtin JSON decoder for dealing with JSON data """
r = requests.get('http://www.json-generator.com/api/json/get/bVVKnZVjpK?indent=2')
print "Getting JSON: ", r # Should be 200 or else if error, then 401 (Unauthorized)
#print r.json()
def versatile_response_codes():
""" You don't have to check for specific status codes (e.g. 200, 404) """
url = "http://httpbin.org/post"
r = requests.get(url)
if r.status_code == requests.codes.ok:
print "Looks okay to me", r.status_code
else:
print "Doesn't look good here", r.status_code
# We can raise an exception if there's a bad request 4XX or 5XX
r.raise_for_status() # Should raise http_error
#(requests.exceptions.HTTPError:)
def accessing_cookies():
"""
You can look at a response's cookies or send your own cookies
to the server
"""
# GET some cookies
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name'] #'example_cookie_value'
# GET and specify your cookies
mycookies = dict(cookies_are='working')
r = requests.get(url, cookies=mycookies)
r.text #'{"cookies": { "cookies_are": "working"}}'
def request_no_redirect():
"""
By default Requests will perform redirects for all verbs except HEAD
Use the 'history' property of the Response to track redirection
Response.history list contains all the Response objects that
were created (sorted oldest to most recent response)
"""
# Redirects by default
r = requests.get('http://github.com') # default Requests allow redirect
print r.url # https://github.com/
print r.status_code # 200
print r.history #[<Response [301]>] # Shows history of a redirect
# Don't allow redirect
r = requests.get('http://github.com', allow_redirects=False)
print r.status_code # 301
print r.history # []
def creating_sessions():
"""
Session objects let you to persist certain parameters across requests.
It also persists cookies across all requests made from the Session
instance
"""
s = requests.Session()
# Sessions let cookies persist across requests
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')
print r.text # {"cookies": {"sessioncookie": 123456789}}
# Sessions can also provide default data to the request methods
# through providing data to the properties on a Session object
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
print s
if __name__ == '__main__':
#get_webpage_details('https://api.github.com/events')
#request_API_calls()
#pass_params_in_urls()
#post_form_data_request()
#pass_headers_in_request()
#response_content()
#json_response_content()
#versatile_response_codes()
#accessing_cookies()
#request_no_redirect()
creating_sessions()