Home > Software engineering >  How to pass body into aiohttp get request?
How to pass body into aiohttp get request?

Time:10-07

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

  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.

CodePudding user response:

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?

  • Related