cle-b / httpdbg

A tool for Python developers to easily debug the HTTP(S) client requests in a Python program.
https://httpdbg.readthedocs.io/
Apache License 2.0
551 stars 13 forks source link

Debug FastAPI app #136

Closed benm5678 closed 7 months ago

benm5678 commented 7 months ago

Hi, is it possible to use pyhttpdbg with FastAPI app? We have something like below that can be fired up with "python main.py" to run the web app. I could not figure out how to get it to run via pyhttpdbg (tried --script approach, and module via this command pyhttpdbg -m uvicorn main:app --host 0.0.0.0 --port 8000 but neither worked). Thanks for any tips!

import uvicorn
from fastapi import FastAPI

app = FastAPI(title="TEST APP")

@app.get("/")
async def root():
    return {"message": "Hello World"}

if __name__ == "__main__":
    """
    Server configurations
    """
    uvicorn.run(
        app="main:app",
        host="0.0.0.0",
        port="8000"
    )
cle-b commented 7 months ago

Hello,

The command pyhttpdbg -m uvicorn main:app --host 0.0.0.0 --port 8000 is correct but that will trace the requests initiated by your app (if any), not the ones received by it.

I plan to add the possibility to trace the requests received on the server side, but not soon.

I keep this issue open until this feature is available.

benm5678 commented 7 months ago

Hi cle-b, that's actually what I need (just reqs initiated by my app) - although I get below exception when trying to launch it with -m uvicorn. Also, another approach that will work even better for us is if the app can dump all the http logs to a file in a way that you can later easily review it with pyhttpdbg (i.e. launch the tool with a log file param).


File "/usr/local/lib/python3.11/site-packages/pydantic/utils.py", line 231, in generate_model_signature
    merged_params[param_name] = Parameter(
                                ^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 2725, in __init__
    raise ValueError('{!r} is not a valid parameter name'.format(name))
ValueError: 'not' is not a valid parameter name```
cle-b commented 7 months ago

I tried again with your example and I can confirm I can trace the HTTP requests initiated by the app when using this command: pyhttpdbg -m uvicorn main:app --host 0.0.0.0 --port 8000. I used requests for the HTTP client.

I've not been able to reproduce your error with packages up-to-date but I tested with an old version of pydantic (1.9.2) as described in this issue and the same error occurred to me. I recommend you to update your fastapi packages if possible. I hope that will help you. For me, it works with Python 3.12 and

Package            Version
------------------ --------
annotated-types    0.6.0
anyio              4.3.0
certifi            2024.2.2
charset-normalizer 3.3.2
click              8.1.7
fastapi            0.110.0
h11                0.14.0
httpdbg            0.18.1
idna               3.6
pip                23.2.1
pydantic           2.6.3
pydantic_core      2.16.3
requests           2.31.0
sniffio            1.3.1
starlette          0.36.3
typing_extensions  4.10.0
urllib3            2.2.1
uvicorn            0.27.1

I don't see anything to fix on httpdbg, sorry.

benm5678 commented 7 months ago

Really appreciate your time for confirming -- I updated my fastapi/pydantic [they were just slightly below your versions] & that resolved it! Good lesson for next time. Thanks a lot!!!