Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request.json() and request.text() return None #2922

Closed
rubenvdham opened this issue Apr 7, 2018 · 5 comments
Closed

Request.json() and request.text() return None #2922

rubenvdham opened this issue Apr 7, 2018 · 5 comments
Labels
invalid This doesn't seem right outdated

Comments

@rubenvdham
Copy link

rubenvdham commented Apr 7, 2018

Long story short

I cannot seem to get the text or json response from a response object.

Expected behaviour

I would be able to extract the json via response.json()

Actual behaviour

response.json() gives a None type
response.text() doesn't give anything either

Steps to reproduce

Run this:

wordpress_url = "https://kominherstel.nl/wp-json/wp/V2/pages/"



async def fetch(session, url):
    async with session.get(url) as response:
        return response

async def main(wordpress_url):
    async with ClientSession() as session:
        response = await fetch(session, wordpress_url)
        print(await response.json())

loop = asyncio.get_event_loop()

loop.run_until_complete(main(wordpress_url))

In a browser "https://kominherstel.nl/wp-json/wp/V2/pages/" will load.

Your environment

aiohttp==2.3.10
OS: Ubuntu 16.04 LTS

@asvetlov
Copy link
Member

asvetlov commented Apr 7, 2018

You cannot fetch response's body after exiting from async with session.get(...) context manager.

@asvetlov asvetlov added the invalid This doesn't seem right label Apr 7, 2018
@asvetlov asvetlov closed this as completed Apr 7, 2018
@rubenvdham
Copy link
Author

rubenvdham commented Apr 7, 2018

Thanks for the advice!
I tried the following instead:

wordpress_url = "https://kominherstel.nl/wp-json/wp/V2/pages/"


async def main(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
            print(await response.json())
            print(await response.text())

            print(response.text)
            print(response.json)

loop = asyncio.get_event_loop()

loop.run_until_complete(main(wordpress_url))

returns the following:


<bound method ClientResponse.text of <ClientResponse(https://kominherstel.nl/wp-json/wp/V2/pages/) [200 OK]>
<CIMultiDictProxy('Date': 'Sat, 07 Apr 2018 20:41:13 GMT', 'Server': 'Apache', 'Vary': 'Cookie', 'X-Robots-Tag': 'noindex', 'Link': '<https://kominherstel.nl/wp-json/>; rel="https://api.w.org/"', 'X-Content-Type-Options': 'nosniff', 'Access-Control-Expose-Headers': 'X-WP-Total, X-WP-TotalPages', 'Access-Control-Allow-Headers': 'Authorization, Content-Type', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Cache-Control': 'no-store, no-cache, must-revalidate', 'Pragma': 'no-cache', 'X-WP-Total': '8', 'X-WP-TotalPages': '1', 'Allow': 'GET', 'Set-Cookie': 'PHPSESSID=9u9an4n6tqt7hkp431ukf5qkk5; path=/', 'Upgrade': 'h2c', 'Connection': 'Upgrade', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json; charset=UTF-8')>
>
<bound method ClientResponse.json of <ClientResponse(https://kominherstel.nl/wp-json/wp/V2/pages/) [200 OK]>
<CIMultiDictProxy('Date': 'Sat, 07 Apr 2018 20:41:13 GMT', 'Server': 'Apache', 'Vary': 'Cookie', 'X-Robots-Tag': 'noindex', 'Link': '<https://kominherstel.nl/wp-json/>; rel="https://api.w.org/"', 'X-Content-Type-Options': 'nosniff', 'Access-Control-Expose-Headers': 'X-WP-Total, X-WP-TotalPages', 'Access-Control-Allow-Headers': 'Authorization, Content-Type', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Cache-Control': 'no-store, no-cache, must-revalidate', 'Pragma': 'no-cache', 'X-WP-Total': '8', 'X-WP-TotalPages': '1', 'Allow': 'GET', 'Set-Cookie': 'PHPSESSID=9u9an4n6tqt7hkp431ukf5qkk5; path=/', 'Upgrade': 'h2c', 'Connection': 'Upgrade', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json; charset=UTF-8')>
>

The first 2 print(await response.json()) and print(await response.text()) should work. From the documentation (https://docs.aiohttp.org/en/stable/client_quickstart.html#json-response-content) it seems the json() method should raise an exception when it fails decoding. However, no exception is raised at all, and there's clearly json at the fetched URL.

@asvetlov
Copy link
Member

asvetlov commented Apr 7, 2018

Sorry, your code doesn't fit attached output.
I've totally missed what behavior do you expect.

@rubenvdham
Copy link
Author

Finally, I found the problem when running the code on windows instead of Ubuntu. On my windows machine, it ran as I expected. The problem: I was still running python 3.5.2 and aiohttp version 2.3.10 on my Ubuntu machine. My bad, I should have checked the version number beforehand:S

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right outdated
Projects
None yet
Development

No branches or pull requests

2 participants