I'm making telegram bot on aiogram and DRF and I need aiohttp function for bot.
So, I've made endpoint and it works well manually, but function return error 404.
There's my code:
class BotUser(models.Model):
name = models.CharField(max_length=128, verbose_name='ФИО')
company_name = models.CharField(max_length=128, verbose_name='Название компании')
phone = models.CharField(max_length=20, verbose_name='Номер')
photo = models.ImageField(upload_to='images', blank=True, verbose_name='Фотография')
activated = models.BooleanField(default=False, verbose_name='Подтверждён')
user_id = models.CharField(max_length=128, unique=True, verbose_name='Телеграм-ID', default='Admin')
username = models.CharField(max_length=128, blank=True, null=True, verbose_name='Телеграм-юзернейм')
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Создан')
last_interaction = models.DateTimeField(null=True, blank=True, verbose_name='Последняя активность')
manager = models.BooleanField(default=False, verbose_name='Менеджер')
city = models.CharField(max_length=64, blank=True, null=True, verbose_name='Город')
def __str__(self):
return f'{self.name}({self.company_name})'
class Meta:
verbose_name = 'Пользователь бота'
verbose_name_plural = 'Пользователи бота'
class BotUserCitySerializer(serializers.ModelSerializer):
class Meta:
model = BotUser
fields = ['city']
class BotUserCityUpdateView(generics.UpdateAPIView):
queryset = BotUser.objects.all()
serializer_class = BotUserCitySerializer
def get_object(self):
user_id = self.kwargs['user_id']
return BotUser.objects.get(user_id=user_id)
path('botusers/<str:user_id>/update-city/', BotUserCityUpdateView.as_view(), name='update-city'),
async def update_botuser_city(user_id, new_city):
async with aiohttp.ClientSession() as session:
url = f'{domen}api/{user_id}/update_city/' # Замените на фактический URL вашего API
data = {'city': new_city}
headers = {
'Content-Type': 'application/json', # Указываем тип контента
'Accept': 'application/json' # Указываем, что ожидаем JSON в ответе
}
async with session.patch(url, json=data, headers=headers) as response:
response_json = await response.json()
return response_json
python services.py
Traceback (most recent call last):
File "C:\Users\hp\desktop\job\bot\services.py", line 703, in <module>
asyncio.run(some_async_function())
File "C:\Users\hp\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\hp\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hp\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\hp\desktop\job\bot\services.py", line 699, in some_async_function
a = await update_botuser_city('679553167', 'Ташкент')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hp\desktop\job\bot\services.py", line 694, in update_botuser_city
response_json = await response.json()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hp\desktop\job\venv\Lib\site-packages\aiohttp\client_reqrep.py", line 1104, in json
raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8', url=URL('http://localhost:8000/api/679553167/update_city/')
I'm expecting to have good response and have no idea why it's not Maybe the problem with my view-function and it's just not good for aiohttp