■ get/put/delete/head/options 함수를 사용해 GET/PUT/DELETE/HEAD/OPTIONS 방식으로 HTTP 호출하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import httpx def printResponse(title, response): print(title ) print(f" response : {response}" ) print(f" response.url : {response.url}" ) print(f" response.headers['content-type'] : {response.headers['content-type']}") print(f" response.encoding : {response.encoding}" ) print(f" response.status_code : {response.status_code}" ) #print(f" response.text : {response.text}" ) print() response = httpx.get("https://httpbin.org/get") printResponse("GET https://httpbin.org/get", response) response = httpx.put("https://httpbin.org/put", data = {"key" : "value"}) printResponse("PUT https://httpbin.org/put", response) response = httpx.delete("https://httpbin.org/delete") printResponse("DELETE https://httpbin.org/delete", response) response = httpx.head("https://httpbin.org/get") printResponse("HEAD https://httpbin.org/get", response) response = httpx.options("https://httpbin.org/get") printResponse("OPTIONS https://httpbin.org/get", response) |
▶ 실행 결과
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
GET https://httpbin.org/get response : <Response [200 OK]> response.url : https://httpbin.org/get response.status_code : 200 response.encoding : ascii response.headers['content-type'] : application/json PUT https://httpbin.org/put response : <Response [200 OK]> response.url : https://httpbin.org/put response.status_code : 200 response.encoding : ascii response.headers['content-type'] : application/json DELETE https://httpbin.org/delete response : <Response [200 OK]> response.url : https://httpbin.org/delete response.status_code : 200 response.encoding : ascii response.headers['content-type'] : application/json HEAD https://httpbin.org/get response : <Response [200 OK]> response.url : https://httpbin.org/get response.status_code : 200 response.encoding : None response.headers['content-type'] : application/json OPTIONS https://httpbin.org/get response : <Response [200 OK]> response.url : https://httpbin.org/get response.status_code : 200 response.encoding : utf-8 response.headers['content-type'] : text/html; charset=utf-8 |