-
Notifications
You must be signed in to change notification settings - Fork 2
/
requests_reqs.py
37 lines (31 loc) · 993 Bytes
/
requests_reqs.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
"""
Module for making requests via the requests library
"""
import requests
def get_request(url):
"""
Performs a 'requests' GET request on the specified URL and returns a JSON
response, or None if it failed
"""
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print(f'The request failed with code {response[0].status_code}')
return None
except Exception as e:
print('A problem occurred with the request')
print(e)
return None
def post_request(url, dict_data):
"""
Performs a 'requests' POST request on the specified URL with the input data,
and returns a JSON response, or None if it failed
"""
response = requests.post(url, json = dict_data)
if response.status_code == 200:
return response.json()
else:
print(f'The request failed with code {response[0].status_code}')
return None