Różnice
Różnice między wybraną wersją a wersją aktualną.
| Nowa wersja | Poprzednia wersja | ||
| programowanie:python:http [2024/08/26 15:52] – utworzono sases | programowanie:python:http [2024/10/09 19:18] (aktualna) – sases | ||
|---|---|---|---|
| Linia 1: | Linia 1: | ||
| ====== Python HTTP ====== | ====== Python HTTP ====== | ||
| + | |||
| + | ===== request ===== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | html = requests.get(" | ||
| + | print(html.text) | ||
| + | </ | ||
| \\ | \\ | ||
| - | [[https://www.w3schools.com/python/module_requests.asp]]\\ | + | \\ |
| - | [[https:// | + | |
| + | ==== get ==== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | url = 'http://example.com/api/resource/' | ||
| + | |||
| + | response = requests.get(url) | ||
| + | |||
| + | if response.status_code == 200: | ||
| + | data = response.json() | ||
| + | print(data) | ||
| + | |||
| + | key_value = data[" | ||
| + | |||
| + | if key_value is not None: | ||
| + | print(key_value) | ||
| + | else: | ||
| + | print(" | ||
| + | else: | ||
| + | print(f' | ||
| + | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | |||
| + | ==== post ==== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | url = 'http://example.com/ | ||
| + | data = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | response = requests.post(url, json=data) | ||
| + | |||
| + | if response.status_code == 201: | ||
| + | print(" | ||
| + | print(response.json()) | ||
| + | else: | ||
| + | print(f' | ||
| + | </code> | ||
| + | \\ | ||
| + | \\ | ||
| + | |||
| + | ==== patch ==== | ||
| + | <code python> | ||
| + | import requests | ||
| + | |||
| + | url = 'http://example.com/ | ||
| + | data = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | response = requests.patch(url, | ||
| + | |||
| + | if response.status_code == 200: | ||
| + | print(" | ||
| + | print(response.json()) | ||
| + | else: | ||
| + | print(f' | ||
| + | </ | ||