Hi there, I would like to add a feature to urllib.error.HTTPError
When we use urllib.request.urlopen
to read the data from a HTTP server and encounter an error, the error traceback only shows the HTTP response status code and the description of the status code, e.g.: HTTPError: HTTP Error 400: Bad Request
. If the HTTP server returns the detailed error message in the response body, the error traceback won’t show it.
For example:
A simple HTTP server that returns 400 with a detailed message.
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get("/")
async def main():
raise HTTPException(
status_code=400,
detail="The format of the parameter is wrong!",
)
return None
When using curl
to get the data from the HTTP server, it shows the status code with the detailed error message.
curl http://127.0.0.1:8000
>>> {"detail":"The format of the parameter is wrong!"}
When using urllib.request.urlopen
to get the data, it only shows the status code.
from urllib.request import urlopen
urlopen("http://127.0.0.1:8000")
Traceback (most recent call last):
......
urllib.error.HTTPError: HTTP Error 400: Bad Request
I think the traceback shown as follows would be very useful for the end-users.
urllib.error.HTTPError: HTTP Error 400: Bad Request, {"detail":"The format of the parameter is wrong!"}
If this feature requests meaningful, I would like to open a PR for this.