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