Python Lernhandbuch

Kapitel 10: REST-APIs nutzen · Abschnitt 3 von 7

POST, PUT, PATCH, DELETE

python
neu = {'title': 'Hallo', 'body': 'Testbeitrag', 'userId': 1}

r = httpx.post('https://jsonplaceholder.typicode.com/posts', json=neu, timeout=10)
print(r.status_code, r.json())     # 201 und der angelegte Datensatz mit 'id'

httpx.put(url, json=vollstaendig)
httpx.patch(url, json={'title': 'Neu'})
httpx.delete(url)
Parameter Wann
json= Dictionary wird als JSON gesendet, Content-Type setzt httpx selbst
data= Formulardaten (application/x-www-form-urlencoded)
files= Datei-Upload, z. B. {'datei': open('a.pdf', 'rb')}
content= Rohe Bytes oder Text

Tipp: Bei Python-Beispielen kannst du auf ▶ Ausprobieren klicken, den Code verändern und direkt ausführen.

← Zurück