G
G
Guerro692020-07-10 21:54:08
Python
Guerro69, 2020-07-10 21:54:08

Do you get an error when trying to send a request to the server via aiohttp?

I am writing code and I need to make a request to the server, but when I run the code I get this error:

File "C:\Users\admin\Desktop\bots_longpoll.py", line 44, in _lp_check
    async with session.post(self.server, data=lp_data) as resp:
TypeError: Constructor parameter should be str

Here is the code:
import aiohttp
import asyncio

class LongPoll:
  def __init__(self, session, wait=25):
    self.token = session[0]
    self.grp_id = session[1]
    self.v = session[-1]
    self.wait = wait
    self.url = 'https://api.vk.com/method/'
    self.ts = None
    self.server = None
    self.key = None
    self.__update_data()

  async def __update_data(self, updts=True):
    api_data = {
      'access_token': self.token,
      'v': self.v,
      'group_id': self.grp_id
      }
    async with aiohttp.ClientSession() as session:
      async with session.post(f"{self.url}groups.getLongPollServer?", data=api_data) as resp:
        r = await resp.json()
        self.server = r['response']['server']
        self.key = r['response']['key']
        if updts:
          self.ts = r['response']['ts']

  async def _lp_check(self):
    lp_data = {
      'act': 'a_check',
      'key': self.key,
      'wait': self.wait,
      'mode': 2,
      'ts': self.ts
    }
    async with aiohttp.ClientSession() as session:
      async with session.post(self.server, data=lp_data) as resp:
        r = await resp.json()
        return r['updates']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Guerro69, 2020-07-10
@Guerro69

Answer to the question:
I forgot that my __update_data function is async -_-, and I had to put await next to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question