HTTPS POST to query FastAPI using python requests
I am trying to serve a Neural Network using FastAPI.
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from typing import Dict

class iRequest(BaseModel):
    arg1: str
    arg2: str

class iResponse(BaseModel):
    pred: str
    probs: Dict[str, float]

@app.post(&quot;/predict&quot;, response_model=iResponse)
def predict(request: iRequest, model: Model = Depends(get_model)):
    pred, probs = model.predict(request.arg1, request.arg2)
    return iResponse(pred = pred, probs = probs)

The manual site http://localhost:8000/docs#/default/predict_predict_post works fine and translates into the following curl command:
curl -X POST &quot;http://localhost:8000/predict&quot; -H  &quot;accept: application/json&quot; -H  &quot;Content-Type: application/json&quot; -d &quot;{\&quot;arg1\&quot;:\&quot;I am the King\&quot;,\&quot;arg2\&quot;:\&quot;You are not my King\&quot;}&quot;

which also works.
When I try to query the API using python requests:
import requests
data = {&quot;arg1&quot;: &quot;I am the King&quot;,
        &quot;arg2&quot;: &quot;You are not my King&quot;}
r = requests.post(&quot;http://localhost:8000/predict&quot;, data=data)

I only get the &quot;422 Unprocessable Entity&quot; Errors. Where am I going wrong here?

You provide a data argument to requests.post, which does a POST with Content-Type: application/x-www-form-urlencoded, which is not JSON.
Consider using requests.post(url, json=data) and you should be fine.
