투케이2K

65. (python/파이썬) [Mac Os] : [requests] : HTTP 통신 모듈 사용해 Post Body Json 요청 및 Response 응답 확인 본문

Python

65. (python/파이썬) [Mac Os] : [requests] : HTTP 통신 모듈 사용해 Post Body Json 요청 및 Response 응답 확인

투케이2K 2024. 5. 4. 09:10
반응형

[개발 환경 설정]

개발 툴 : VsCode

개발 언어 : python

 

[소스 코드]

# --------------------------------------------------------------
# [import]
# --------------------------------------------------------------
import requests
import json
# --------------------------------------------------------------



# --------------------------------------------------------------
# [요약 설명]
# --------------------------------------------------------------
# 1. import requests : 파이썬에서 HTTP Request 요청을 할 때 사용합니다
# --------------------------------------------------------------
# 2. 모듈 설치 방법 참고 사이트 : https://blog.naver.com/kkh0977/223435974300
# --------------------------------------------------------------
# 3. import json : 파이썬에서 json 을 다룰 때 사용할 수 있습니다
# --------------------------------------------------------------



# --------------------------------------------------------------
# [class start]
# --------------------------------------------------------------



# --------------------------------------------------------------
# [main start]
# --------------------------------------------------------------


# [HTTP 통신 URL 주소 지정]
url = 'https://jsonplaceholder.typicode.com/posts'


# [HTTP 요청 헤더 headers 정의]
headers = {
	'Content-Type': 'application/json'
}


# [HTTP 요청 타임 아웃 시간 정의]
timeOut = 30


# [HTTP 요청 Body 데이터 정의]
bodyData = {
    'userId': 1,
    'id': 1
}


# [HTTP 요청 수행 및 응답 데이터 확인]
response = requests.post(url, headers=headers, data=json.dumps(bodyData), timeout=timeOut)

responseHeaders = response.headers
statusCode = response.status_code
responseData = response.text


# [로그 출력]
print("")
print("----------------------------------------")
print("url :: ", url)
print("----------------------------------------")
print("headers :: ", headers)
print("----------------------------------------")
print("bodyData :: ", bodyData)
print("----------------------------------------")
print("responseHeaders :: ", responseHeaders)
print("----------------------------------------")
print("statusCode :: ", statusCode)
print("----------------------------------------")
print("responseData :: ", responseData)
print("----------------------------------------")
print("")


# --------------------------------------------------------------
# [main end]
# --------------------------------------------------------------



# --------------------------------------------------------------
# [class end]
# --------------------------------------------------------------
 

[결과 출력]


반응형
Comments