FastAPI POST endpoint returning "Field required" when sending dictionary from Jython

Hi everyone,


I'm trying to use a POST method:

h = {"accept": "application/json", "Content-Type": "application/json"}

client = system.net.httpClient()
response = client.post(url, data=data, headers=h)
response.json

data is a dictionary.

I get this error:

{'detail': [{'msg': 'Field required', 'loc': ['body'], 'type': 'missing', 'input': None}]}

For context, I’m using FastAPI for my API.

The endpoint is defined like this:

@app.post("/item")
def update_item(new_item: Item):
    ... 

And the Item model looks like this:

class Item(BaseModel):
    t_stamp: str
    temp_1: float
    hum_1: float
    temp_2: float
    hum_2: float
    temp_3: float
    hum_3: float
    temp_4: float
    hum_4: float
    temp_5: float
    hum_5: float
    temp_6: float
    hum_6: float


The perplexing part is that if I use httpPost instead, it works fine:

import json

# Convert dictionary to JSON string
json_string = json.dumps(data)

# Use httpPost directly
response = system.net.httpPost(url, contentType="application/json", postData=json_string)

Thanks in advance for any help or advice

what happens if you try passing in json.dumps(data) to httpClient?

Can you share the actual dictionary you're sending?

What if you send your data to a testing tool like that echoes what you requested back to you?
I like http://httpbin.org/, though it appears to be down at the moment. It's self hostable via Docker: https://hub.docker.com/r/kennethreitz/httpbin/