-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
98 lines (69 loc) · 2.48 KB
/
example.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
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
import requests
import json
# Simple example of using Python to make API calls, useful for testing microservices...
# Using these calls to test with: https://docs.postman-echo.com/?version=latest
# Requests homepage: https://2.python-requests.org/en/master/
token = None
header = {
"Content-Type": "application/json",
"Authorization" : "Bearer " + token,
}
def PrintJson( jsontext ):
parsed = json.loads( jsontext )
print( json.dumps( parsed, indent=4, sort_keys=True ) )
def Authenticate( header ):
header = { "Content-Type": "application/json" }
url = ""
body = {
}
bodyJson = json.dumps( body )
response = response.post( url, data=bodyJson, headers=header )
print( response.status_code )
PrintJson( response.content )
token = response.json()["access_token"]
return token
# token = Authenticate( header )
# - POST requests ---------------------------------------------------- #
def PostRequest( header ):
print( "\n\n Post request..." )
url = "https://postman-echo.com/post"
body = {
"item1" : "value1"
}
bodyJson = json.dumps( body )
response = requests.post( url, data=bodyJson, headers=header )
print( response.status_code )
PrintJson( response.content )
PostRequest( header )
# - GET requests ----------------------------------------------------- #
def GetRequest( header ):
print( "\n\n Get request..." )
url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
response = requests.get( url, headers=header )
print( response.status_code )
PrintJson( response.content )
GetRequest( header )
# - PUT requests ----------------------------------------------------- #
def PutRequest( header ):
print( "\n\n Put request..." )
url = "https://postman-echo.com/put"
body = {
"item1" : "value1"
}
bodyJson = json.dumps( body )
response = requests.put( url, data=bodyJson, headers=header )
print( response.status_code )
PrintJson( response.content )
PutRequest( header )
# - PATCH requests --------------------------------------------------- #
def PatchRequest( header ):
print( "\n\n Patch request..." )
url = "https://postman-echo.com/patch"
body = {
"item1" : "value1"
}
bodyJson = json.dumps( body )
response = requests.patch( url, data=bodyJson, headers=header )
print( response.status_code )
PrintJson( response.content )
PatchRequest( header )