■ Response 클래스의 raise_for_status 메소드를 사용해 에러를 발생시키는 방법을 보여준다.
▶ 예제 코드 (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 34 35 36 37 |
from httpx import get, codes 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 = get("https://httpbin.org/status/404") printResponse("404 에러 수신시 에러 발생시키기", response) if(response.status_code == codes.NOT_FOUND): response.raise_for_status() """ 404 에러 수신시 에러 발생시키기 response : <Response [404 NOT FOUND]> response.url : https://httpbin.org/status/404 response.headers['content-type'] : text/html; charset=utf-8 response.encoding : utf-8 response.status_code : 404 Traceback (most recent call last): File "/home/king/testproject/main2.py", line 18, in <module> response.raise_for_status() File "/home/king/testproject/env/lib/python3.10/site-packages/httpx/_models.py", line 1510, in raise_for_status raise HTTPStatusError(message, request=request, response=self) httpx.HTTPStatusError: Client error '404 NOT FOUND' for url 'https://httpbin.org/status/404' For more information check: https://httpstatuses.com/404 """ |