aiohttp.ClientSession doesn't seem to post the data (username, password, logintoken) that I give it

Viewed 378

I've been trying to turn one of my programs async, but the Session doesn't seem to post the data.

from aiohttp import ClientSession as Session
from bs4 import BeautifulSoup as BS
import asyncio

class Finder(Session):
    async def login(self, loginurl, homeurl, username, password):
        async def getlogintoken(loginurl):
            async with self.get(loginurl) as loginpage:
                return(BS(await loginpage.text(), 'html.parser').select('#login > input[type=hidden]:nth-child(3)')[0]['value'])

        async def postlogindata(loginurl, username, password, logintoken):
            await self.post(loginurl, data={'username': username, 'password': password, 'logintoken': logintoken})

        async def gethome(homeurl):
            async with self.get(homeurl) as homepage:
                print(homepage.url)

        logintoken = await getlogintoken(loginurl)
        print(logintoken)
        await postlogindata(loginurl, username, password, logintoken)
        await gethome(homeurl)

This is what I have and it works perfectly fine with requests (without all the async stuff), but with aiohttp it doesn't seem to work. Any help would be greatly appreciated!

1 Answers

I figured out what was wrong: The ClientSession doesn't save cookies automatically. It posts correctly but in order to keep the session ID you have to update cookies when getting the logintoken.

async with self.get(loginurl) as loginpage:
            self.cookie_jar.update_cookies(loginpage.cookies)
            return BS(await loginpage.text(), 'html.parser').find(attrs={'name': 'token'})['value']
Related