Testing the post request of Fastapi.
But, every time I get 422 Unprocessable Entity Error.
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
print(item)
return item
Running it on gcp using : uvicorn testing:app --host 0.0.0.0 --port 5000
Request Structure:
Post Request on the route:http://35.132.89.173:5000/items/
body: { "name": "nmsaey", "price":34}
header: { Content-Type: application/json}
Version:
python: 3.6.9
fastapi: 0.60.1
pydantic: 1.6.1
The payload { "name": "nmsaey", price:34} doesn't seem to be properly json formatted.
Try to put price in quotation marks: {"name": "nmsaey", "price": 34}
@marodev Sorry, it was just a typo.
price is in quotes. I will update the question.
How do you send the HTTP POST request? With a tool like Postman or by using a Python script?
@marodev
Through Postman
It works just fine here, command used to test:
curl -X POST "http://0.0.0.0:8000/items/" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"name\":\"nmsaey\",\"price\":34}"
Running it on gcp
I'll assume you are running it inside a virtual machine since the host looks very similar to what a vm makes http://35.132.89.173:5000/items/
Make sure when creating the vm you click the 'Allow HTTP traffic' option
If that doesn't work check if you still have the vm ssh terminal open # yes dumb but from personal experience (:man_facepalming:) you should try
Works for me as well. Does it look similar to this:


Thanks for your response
The problem was in Postman.
Everything was fine using code.
Thanks for the help here everyone! :clap: :bow:
Thanks for reporting back and closing the issue :+1:
Most helpful comment
The payload
{ "name": "nmsaey", price:34}doesn't seem to be properly json formatted.Try to put price in quotation marks:
{"name": "nmsaey", "price": 34}