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.

How do I reference “params”?

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

This is the output of this test:

poetry run pytest -xsk task1

async_client = <httpx.AsyncClient object at 0x1076cb190>

    @pytest.mark.asyncio
    async def test_task1(async_client):
        response = await async_client.get("/task1/greet/Jasmin")
        assert response.status_code == 200
        assert response.json() == "Hallo Jasmin, ich bin Emilia."
    
        response = await async_client.get("/task1/greet/Stefan", params={"language": "en"})
        assert response.status_code == 200
>       assert response.json() == "Hello Stefan, I am Emilia."
E       AssertionError: assert 'Hallo Stefan...h bin Emilia.' == 'Hello Stefan, I am Emilia.'
E         - Hello Stefan, I am Emilia.
E         ?  ^            ^^^^
E         + Hallo Stefan, ich bin Emilia.
E         ?  ^            ^^^^^^^

test_emilia.py:22: AssertionError

I am supposed to edit this code:

@app.get("/task1/greet/{name}", tags=["Task 1"], summary="👋🇩🇪🇬🇧🇪🇸")
async def task1_greet(name: str) -> str:
    """Greet somebody in German, English or Spanish!"""
    
    return f"Hallo {name}, ich bin Emilia."

I don’t understand how to reference the “params={“language”:”en”}” in the test above. Assuming it’s being sent by the method “async_client.get()”,how do I pick that up in the method “task1_greet”?

Do I make a method argument like **kwargs, perhaps?

Thanks very much

Description

I need to write code that returns text depending on the language passed in “params”, and I don’t know how to reference “params” because I don’t understand how async_client.get corresponds to async def task1_greet.

I will explore kwargs but I’d still like to understand better how these two methods interact with each other.

Thank you

Operating System

macOS

Operating System Details

No response

FastAPI Version

latest

Python Version

latest

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
rogerbrinkmanncommented, Feb 8, 2022

@peterelbert , take a look at this line of AlrasheedA’s code:

async def task1_greet(name: str, language: str = None) -> str:

str gets the default value of None, making it optional, in case the language parameter isn’t set.

0reactions
AlrasheedAcommented, Feb 10, 2022

@peterelbert Please close the issue if it worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

params keyword for parameter arrays - C# reference
By using the params keyword, you can specify a method parameter that takes a variable number of arguments. The parameter type must be...
Read more >
C# Pass By Reference (Ref) with Examples - Tutlane
In c# passing parameter by reference means passing a reference of variable to the method. ... C# Pass By Reference · C# Out...
Read more >
How to pass ref parameters in params - Stack Overflow
I want to pass n number of parameters to a method (both reference and normal). Here is my source code static void testParams(params...
Read more >
params | Data Version Control - DVC
positional arguments: COMMAND diff Show changes in params between commits in the DVC repository, or between a commit and the workspace. Description. Parameters ......
Read more >
Parameter-Passing Modes - cs.wisc.edu
Parameter a is a value parameter, b is a reference parameter, and c is a const-reference parameter. Value Parameters. When a parameter is...
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