How to use the fastapi.Body function in fastapi

To help you get started, we’ve selected a few fastapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github smagafurov / fastapi-jsonrpc / tests / test_openapi.py View on Github external
def probe(
        data: List[str] = Body(..., example=['111', '222']),
        amount: int = Body(..., gt=5, example=10),
    ) -> List[int]:
        del data, amount
        return [1, 2, 3]
github smagafurov / fastapi-jsonrpc / tests / test_openapi.py View on Github external
def probe(
        data: List[str] = Body(..., example=['111', '222']),
        amount: int = Body(..., gt=5, example=10),
    ) -> List[int]:
        del data, amount
        return [1, 2, 3]
github nsidnev / fastapi-realworld-example-app / app / api / routes / authentication.py View on Github external
async def register(
    user_create: UserInCreate = Body(..., embed=True, alias="user"),
    users_repo: UsersRepository = Depends(get_repository(UsersRepository)),
) -> UserInResponse:
    if await check_username_is_taken(users_repo, user_create.username):
        raise HTTPException(
            status_code=HTTP_400_BAD_REQUEST, detail=strings.USERNAME_TAKEN
        )

    if await check_email_is_taken(users_repo, user_create.email):
        raise HTTPException(
            status_code=HTTP_400_BAD_REQUEST, detail=strings.EMAIL_TAKEN
        )

    user = await users_repo.create_user(**user_create.dict())

    token = jwt.create_access_token_for_user(user, str(config.SECRET_KEY))
    return UserInResponse(
github nsidnev / fastapi-realworld-example-app / app / api / api_v1 / endpoints / user.py View on Github external
async def update_current_user(
    user: UserInUpdate = Body(..., embed=True),
    current_user: User = Depends(get_current_user_authorizer()),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        if user.username == current_user.username:
            user.username = None
        if user.email == current_user.email:
            user.email = None

        await check_free_username_and_email(conn, user.username, user.email)

        async with conn.transaction():
            dbuser = await update_user(conn, current_user.username, user)
            return UserInResponse(user=User(**dbuser.dict(), token=current_user.token))
github QwantResearch / idunn / idunn / api / geocoder.py View on Github external
async def get_autocomplete(
    query: QueryParams = Depends(QueryParams), extra: ExtraParams = Body(ExtraParams())
):
    async def get_intentions():
        if query.lang not in nlu_allowed_languages:
            return None

        if not query.nlu and not autocomplete_nlu_shadow_enabled:
            return None

        focus = None
        if query.lon and query.lat:
            focus = Point(query.lon, query.lat)

        return await nlu_client.get_intentions(text=query.q, lang=query.lang, focus=focus)

    autocomplete_response, intentions = await asyncio.gather(
        bragi_client.autocomplete(query, extra), get_intentions()
github erm / guitarlette / src / app.py View on Github external
def parse(content: str = Body(..., embed=True)):
    parser = Song(content)
    return {"html_content": parser.html}
github tiangolo / full-stack-fastapi-postgresql / {{cookiecutter.project_slug}} / backend / app / app / api / api_v1 / endpoints / login.py View on Github external
def reset_password(token: str = Body(...), new_password: str = Body(...), db: Session = Depends(get_db)):
    """
    Reset password
    """
    email = verify_password_reset_token(token)
    if not email:
        raise HTTPException(status_code=400, detail="Invalid token")
    user = crud.user.get_by_email(db, email=email)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    hashed_password = get_password_hash(new_password)
    user.hashed_password = hashed_password
github tiangolo / full-stack-fastapi-postgresql / {{cookiecutter.project_slug}} / backend / app / app / api / api_v1 / endpoints / users.py View on Github external
def update_user_me(
    *,
    db: Session = Depends(get_db),
    password: str = Body(None),
    full_name: str = Body(None),
    email: EmailStr = Body(None),
    current_user: DBUser = Depends(get_current_active_user),
):
    """
    Update own user.
    """
    current_user_data = jsonable_encoder(current_user)
    user_in = UserUpdate(**current_user_data)
    if password is not None:
        user_in.password = password
    if full_name is not None:
        user_in.full_name = full_name
    if email is not None:
        user_in.email = email
    user = crud.user.update(db, user=current_user, user_in=user_in)
    return user
github nsidnev / fastapi-realworld-example-app / app / api / routes / users.py View on Github external
async def update_current_user(
    user_update: UserInUpdate = Body(..., embed=True, alias="user"),
    current_user: User = Depends(get_current_user_authorizer()),
    users_repo: UsersRepository = Depends(get_repository(UsersRepository)),
) -> UserInResponse:
    if user_update.username and user_update.username != current_user.username:
        if await check_username_is_taken(users_repo, user_update.username):
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST, detail=strings.USERNAME_TAKEN
            )

    if user_update.email and user_update.email != current_user.email:
        if await check_email_is_taken(users_repo, user_update.email):
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST, detail=strings.EMAIL_TAKEN
            )

    user = await users_repo.update_user(user=current_user, **user_update.dict())
github nsidnev / fastapi-realworld-example-app / app / api / routes / authentication.py View on Github external
async def login(
    user_login: UserInLogin = Body(..., embed=True, alias="user"),
    users_repo: UsersRepository = Depends(get_repository(UsersRepository)),
) -> UserInResponse:
    wrong_login_error = HTTPException(
        status_code=HTTP_400_BAD_REQUEST, detail=strings.INCORRECT_LOGIN_INPUT
    )

    try:
        user = await users_repo.get_user_by_email(email=user_login.email)
    except EntityDoesNotExist as existence_error:
        raise wrong_login_error from existence_error

    if not user.check_password(user_login.password):
        raise wrong_login_error

    token = jwt.create_access_token_for_user(user, str(config.SECRET_KEY))
    return UserInResponse(