Sending request body to fastapi server while debugging

Viewed 718

I have a simple fastapi server which is supposed to accept a request body that consists of a single string. It is supposed to take that string and send it to another module and get the return result, then send that back to the client.

Things on the client side seem to be working fine, but for some reason the call from the server to the other module isn't working. I suspect that I am not actually sending to the module what I think I am, and that I have to modify the call in some way. I want to stop it in the debugger so I can see exactly what the request and response objects look like.

I know how to set it up to debug the server, but not how to then send it a request body while in the debugger.

The server looks like this, with a main block added so I can debug it directly:

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from annotate import AnnotationModule
from fastapi.testclient import TestClient

app = FastAPI()

class Sentence(BaseModel):
    text: str

print('Spinning up resources.')
am = AnnotationModule(wsd_only=True)
print('Everything loaded. Ready.')

@app.post("/annotate/")
async def read_item(sentence: Sentence):
    return am.annotate(sentence.text)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

I can set a breakpoint and run this, but how do I send it a request body so I can see it working?

I tried using this client code, but it didn't work:

import requests

# api-endpoint
URL = "http://0.0.0.0:8000/annotate"
PARAMS = {"text": "This is a test sentence."}
  
# sending get request and saving the response as response object
annotation_dict = requests.post(url = URL, json = PARAMS)

Running this while the server is sitting in the debugger, I get:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=8000): Max retries exceeded with url: /annotate (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd926ed5f90>: Failed to establish a new connection: [Errno 61] Connection refused'))

So, how can I debug it while observing it taking and processing a request? If it matters, I am doing this in vscode.

0 Answers
Related