Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to post JSON data/payload (to a FastAPI python server app) #1066

Closed
TuxxuT opened this issue Jun 15, 2024 · 3 comments
Closed

Correct way to post JSON data/payload (to a FastAPI python server app) #1066

TuxxuT opened this issue Jun 15, 2024 · 3 comments

Comments

@TuxxuT
Copy link

TuxxuT commented Jun 15, 2024

Description

Please, I want to know what is the correct way to send JSON data to fastAPI app?
I tried many many things, but nothing seems to work...

Example/How to Reproduce

For example:

Python part (server side)

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    vorname: str
    nachname: str | None = None
    alter: int
    gewicht: float | None = None

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

The JSON payload data has this structure

{
"vorname": "string",
"nachname": "string",
"alter": 0,
"gewicht": 0
}

CPP part (client):

// POST REQUEST
    cpr::Response postRequest = cpr::Post(cpr::Url{"http://127.0.0.1:8000/items"},
                    cpr::Header{{"Content-Type", "application/json"}},
                   cpr::Payload{ 
                        {"vorname", "test"}, 
                        {"nachname", "test"}, 
                        {"alter", "12"}, {"gewicht", "35"} });
    std::cout << postRequest.text << std::endl;

Unfortuntly this does not work.
When I run the cpp executable I get the following error message:
{"detail":[{"type":"json_invalid","loc":["body",0],"msg":"JSON decode error","input":{},"ctx":{"error":"Expecting value"}}]}

Possible Fix

No response

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

  • OS: Arch Linux
  • Version:
@TuxxuT
Copy link
Author

TuxxuT commented Jun 15, 2024

This also does not work:

std::string json_data{" \"vorname\": \"string\", \"nachname\": \"string\", \"alter\": 0, \"gewicht\": 0 "};

    // POST REQUEST
    cpr::Response postRequest = cpr::Post(cpr::Url{"http://127.0.0.1:8000/items"},
                    cpr::Header{{"Content-Type", "text/plain"}},
                   cpr::Body{json_data});
    std::cout << postRequest.text << std::endl;

    }

@COM8
Copy link
Member

COM8 commented Jun 15, 2024

May I suggest:

int main() {
    // Setup curl share
    cpr::Response postRequest = cpr::Post(cpr::Url{"http://127.0.0.1:8000/items"},
                                          cpr::Header{{"Content-Type", "application/json"}},
                                          cpr::Body{R"({ "vorname": "Olaf", "nachname": "Ralf", "alter": 42, "gewicht": 24 })"});
    std::cout << postRequest.text << std::endl;

    return 0;
}

The first one is invalid since there you send the numbers as string and the second example misses the { and }.

@TuxxuT
Copy link
Author

TuxxuT commented Jun 15, 2024

Thank you so much! Problem solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants