-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (50 loc) · 1.75 KB
/
main.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
import requests
import httpx
def make_post_request():
# The URL we're sending the POST request to
url = 'https://httpbin.org/post'
# Sample data to send in the POST request
data = {
'name': 'John Doe',
'age': 30,
'message': 'Hello, World!'
}
try:
# Make the POST request
response = requests.post(url, json=data)
# Ensure the request was successful
response.raise_for_status()
# Print the response
print("Response Status Code:", response.status_code)
print("\nResponse Content:")
print(response.json())
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
def make_post_request_httpx():
# The URL we're sending the POST request to
url = 'https://httpbin.org/post'
# Sample data to send in the POST request
data = {
'name': 'John Doe',
'age': 30,
'message': 'Hello, World!'
}
try:
# Create an httpx client
with httpx.Client() as client:
# Make the POST request
response = client.post(url, json=data)
# Ensure the request was successful
response.raise_for_status()
# Print the response
print("Response Status Code:", response.status_code)
print("\nResponse Content:")
print(response.json())
except httpx.RequestError as e:
print(f"An error occurred while making the request: {e}")
except httpx.HTTPStatusError as e:
print(f"HTTP Error occurred: {e}")
if __name__ == "__main__":
make_post_request()
print("\nNow trying with httpx:")
make_post_request_httpx()