7
async def method(request):
    ## here how to get query parameters
    param1 = request.rel_url.query['name']
    param2 = request.rel_url.query['age']
    return web.Response(text=str(result))


if __name__ == '__main__':
    app = web.Application()
    app.router.add_route('GET', "/sample", method)    

    web.run_app(app,host='localhost', port=3000)

The above code is running in python 3.6. I need to extract the query parameter values (xyz & xy) from the sample URL http://localhost.com/sample?name=xyz&age=xy. I Tried with req.rel_url.query and also with request.query_string. It was giving only the first parameter value xyz, but I was not getting xy (age) which was the second parameter in the query.

How can I get both the query values?

2
  • Here are some changes to my snippet. Commented Dec 17, 2017 at 1:18
  • 1. localhost was changed to my machine IP , so in query http://<IP Adress>:3000/sample?name=xyz&age=xy 2. business logic for result was not added in the snippet. Since I was failing in request.rel_url.query['age']. Commented Dec 17, 2017 at 1:21

3 Answers 3

16

You have few errors here.

  1. result is not defined in your function. You get params right way, but then error occurs as result is not defined
  2. You're targeting localhost.com, not sure how that is set on your machine, but it shouldn't work.

So, here is working example:

from aiohttp import web

async def method(request):
    ## here how to get query parameters
    param1 = request.rel_url.query['name']
    param2 = request.rel_url.query['age']
    result = "name: {}, age: {}".format(param1, param2)
    return web.Response(text=str(result))


if __name__ == '__main__':
    app = web.Application()
    app.router.add_route('GET', "/sample", method)

    web.run_app(app,host='localhost', port=11111)

then you can try: http://localhost:11111/sample?name=xyz&age=xy and it is working.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried with the above snippet. I was getting an error of KeyError: "Key not found: 'age'". The business logic for result I have not added. But the issue I was facing was, I was not able to read the second parameter value(In this case it was age )
Local host was changed to my machine IP address
return web.Response(text=str(result)) str function is not required, because format method returns the string already.
Understand this is a old post, but this is exactly what I am trying to do and it works! BUT how do I stop web.run_app once it returns the result? Thanks!
Not sure, this was long time ago. You can check documentation to see if it helps: docs.aiohttp.org/en/stable/web_advanced.html#graceful-shutdown
2

Quick example:

async def get_handler(self, request):

    param1 = request.rel_url.query.get('name', '')
    param2 = request.rel_url.query.get('age', '')

    return web.Response(text=f"Name: {param1}, Age: {param2}")

2 Comments

Needs more detail, you crash your script if the parameter doesn't exist in the above example
I understand your concern, but SO answers should address the main question with a very simple example, not a full working program. The developer should be able to understand and change the example accordingly to their needs.
1

The topic is old but I have encountered a similar problem: turns out the issue didn't come from the server but the client:

  • curl http://localhost.com/sample?name=xyz&age=xy was indeed sending only the first parameter.
  • However curl "http://localhost.com/sample?name=xyz&age=xy" gave the desired result.

Make sure the parameters are well sent with the request

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.