Skip to main content

Python Örnekleri

Python (requests) ile API istekleri yapmak için örnekler.

Kurulum

pip install requests

Temel Kullanım

GET İsteği

import requests

headers = {
'Authorization': 'Bearer YOUR_TOKEN',
'Content-Type': 'application/json'
}

response = requests.get('https://api.example.com/api/v1/users', headers=headers)
data = response.json()
print(data)

POST İsteği

response = requests.post(
'https://api.example.com/api/v1/users',
headers=headers,
json={
'name': 'John Doe',
'email': 'john@example.com'
}
)

data = response.json()
print(data)

PUT İsteği

response = requests.put(
f'https://api.example.com/api/v1/users/{uuid}',
headers=headers,
json={
'name': 'Jane Doe'
}
)

data = response.json()
print(data)

DELETE İsteği

response = requests.delete(
f'https://api.example.com/api/v1/users/{uuid}',
headers=headers
)

data = response.json()
print(data)

Login Örneği

response = requests.post(
'https://api.example.com/api/v1/login',
json={
'email': 'admin@example.com',
'password': 'secret123',
'device_name': 'MyDevice'
}
)

data = response.json()
token = data['token']

Hata Yönetimi

try:
response = requests.get('https://api.example.com/api/v1/users', headers=headers)
response.raise_for_status()
data = response.json()
print(data)
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")

İlgili Dokümantasyon