-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
26 lines (23 loc) · 908 Bytes
/
index.html
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
import requests
def fetch_url(url, method='GET', data=None, headers=None):
"""
Fetches data from a given URL using GET or POST method.
:param url: URL to fetch data from.
:param method: HTTP method ('GET' or 'POST').
:param data: Data for POST request.
:param headers: Additional headers if required.
:return: Response text or error message.
"""
try:
if method.upper() == 'POST':
response = requests.post(url, data=data, headers=headers)
else:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx)
return response.text
except requests.exceptions.RequestException as e:
return f"Error: {e}"
# Example Usage
if _name_ == "_main_":
test_url = "https://jsonplaceholder.typicode.com/posts/1"
print(fetch_url(test_url))