Python Forum
Error on basic Post API, FastAPI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error on basic Post API, FastAPI
#1
Hello, I am writing a POST API.
My intention is to retrieve an IP address and save into a database.

When I go to Postman and I hit the API endpoint, I am getting the following from Postman:
http://127.0.0.1:8000/ip
In the Body I put:
Key=IP value=1.1.1.1
Postman answer:
{
"detail": [
{
"loc": [
"body"
],
"msg": "value is not a valid dict",
"type": "type_error.dict"
}
]
}

Code:
from fastapi import FastAPI
from pydantic import BaseModel


class Item(BaseModel):
    IP: str

app = FastAPI()

@app.post("/ip")
async def create_item(item: Item):

    database = "apix.db"                                                                                                                                                                            
    table = "APIW"
    savetodb(database, table, item)

    return {'ip':item}


def savetodb(database, table, ip):

	connection_obj = sqlite3.connect(database)
	cursor_obj = connection_obj.cursor()
	cursor_obj.execute("DROP TABLE IF EXISTS " + table)
	table = """ CREATE TABLE """ + table + """ (
				 VARCHAR(255) NOT NULL
			); """
	
	cursor_obj.execute(table)
    #For now hardcoding this
	connection_obj.execute ("""INSERT INTO APIW (IP) VALUES ("1.1.1.1")""")
    #connection_obj.execute ("""INSERT INTO """ +table+ """(IP) VALUES (""" + ip + ")""")
	connection_obj.commit()
	connection_obj.close()
Reply
#2
You forgot to include your question.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with api structure and logging. (uvicorn and fastapi) KaranTrivedi 0 2,073 Sep-08-2020, 02:12 PM
Last Post: KaranTrivedi
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,010 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Basic subtraction error rix 4 3,423 Oct-11-2019, 06:43 AM
Last Post: buran
  No route to host error when using requests.post in python on raspberry pi mariummalik22 0 4,070 Jan-06-2018, 08:34 PM
Last Post: mariummalik22
  First Post/ Class Error Question jvan1601 2 3,439 May-01-2017, 04:21 AM
Last Post: jvan1601

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020