기계는 거짓말하지 않는다

Python requests 모듈 JSON 데이터 POST 전송 예제 본문

Python

Python requests 모듈 JSON 데이터 POST 전송 예제

KillinTime 2024. 1. 21. 22:28

Python requests 모듈로 JSON 데이터를 POST 방식으로 전송하는 간략한 예제이다.

8000번 포트의 URL로 JSON 데이터를 전송한다.

import requests

# JSON 데이터
data_to_send = {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
}

# 전송 URL, 8000번 포트 사용
url = "https://example.com:8000/api"

# JSON 데이터를 POST 요청으로 전송
response = requests.post(url, json=data_to_send)

# 응답 확인
if response.status_code == 200:
    print("success")
    print("response data:", response.json())
else:
    print("failed. status code:", response.status_code)
    print("error message:", response.text)
Comments