question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION]Using a single Body parameter, the JSON format will report an error

See original GitHub issue

Description

Using a single Body parameter, the JSON format will report an error

Additional context

@router.post("/test/")
async def test(abd: str=Body(...)):
    print(abd)               ---error

@router.post("/test/")
async def test(abd: str=Body(...), acd: str=Body(...)):
    print(abd)               ---right

Using a single Body parameter, the JSON format will report an error

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BillGrimcommented, Apr 13, 2020

@ wxq0309您如何解决此问题?我有相同的问题

This can be done by setting the Embed property

item: Item = Body(..., embed=True)

example:
from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str = None
    price: float
    tax: float = None


@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item = Body(..., embed=True)):
    results = {"item_id": item_id, "item": item}
    return results

In my opinion, the condition of your solution is already different from the problem you met, which is “Single parameter declared with fastapi.Body requests str formatted rather than json formatted request body”.

I read the official doc singular-values-in-body, but the demo contains extra paramters(request param, pydantic model).

In my case, I have many apis that needs only a single post body param.

So Is there a way to declare a single param in body of a post api without using pydantic?

0reactions
tiangolocommented, Apr 6, 2020

Thanks for the discussion here everyone! And thanks @wxq0309 for closing the issue and reporting back your solution. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

I've received an "Invalid JSON string" error. What do I do?
What do I do? An invalid JSON error can occur for many reasons. The most common issue is usually associated with a file...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
Measurement Protocol Reference - Google Developers
Data is sent to Google Analytics using the Measurement Protocol Data in two parts: Query parameters; A JSON POST body. query parameters. Parameter...
Read more >
Geopoints—ArcGIS Survey123 | Documentation
Each repeat in a survey can also have more than one geopoint, geotrace, ... displays WGS84 latitude and longitude values in degrees/minutes/seconds format....
Read more >
fastapi - How to read body as any valid json? - Stack Overflow
You can find nearly everything inside the Request object. You are able to get request body with request.json() , which will give you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found