Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass body into aiohttp get request?

How do I pass a body into aiohttp’s get requests?

The code I have below is what I want to run asynchronously but im not able to get the same response –

using requests

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  request_accounts = requests.get(
        "{hostname}/audit/events".format(
            hostname=settings.RM_BASE_URL
        ),
        json={
            "type": {
                "id": 2
            },
            "dut": {
                "hardware": {
                    "mac_address": mac_address
                }
            }
        }
    )

The above code returns the right response, but the below doesn’t –

async def main():
    json={
            "type": {
                "id": 2
            },
            "dut": {
                "hardware": {
                    "mac_address": mac_address
                }
            }
        }
    async with aiohttp.ClientSession() as session:
        async with session.get(f"{settings.RM_BASE_URL}/audit/events", params=json) as resp:
            print(resp.status)
            print(await resp.text())

asyncio.run(main())

does aiohttp support passing a body through a get request?

any help would be appreciated.

>Solution :

You can pass a body using session.request(), which is the the backend of session.get() as detailed here:

How to send GET requests with JSON body using requests 2.18?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading