D
D
dr_mamaev2018-08-23 18:55:48
Python
dr_mamaev, 2018-08-23 18:55:48

Url address in get request in python 3.6 is truncated, why?

There is a simple get request

import requests
def send_message (peer_id,message='',attachment='',forward_messages='',token=""):
    r=requests.get('https://api.vk.com/method/messages.send',params={'peer_id':peer_id,'message':message,'attachment':attachment,'forward_messages':forward_messages,'v':'5.71','access_token':token})
    print((r.content).decode('utf-8'))

When trying to navigate to a very long address, over 2500 characters or so, there are problems. As a result, instead of a normal API response, I get this:
send_message (2000000723,'t'*4000)
<html>
<head><title>414 Request-URI Too Long</title></head>
<body bgcolor="white">
<center><h1>414 Request-URI Too Long</h1></center>
<hr><center>nginx/0.3.33</center>
</body>
</html>

I tried the same thing through the "VK" library, everything is sent normally, in the chrome browser the link opens and the server response is returned, with "requests" what? How to fix this misunderstanding

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladlen Hellsite, 2018-08-23
@dr_mamaev

Send data via POST, not GET. Usually a GET request is limited to 64k, but depends on the web server settings.

import requests
def send_message (peer_id,message='',attachment='',forward_messages='',token=""):
    r=requests.post('https://api.vk.com/method/messages.send', data ={'peer_id':peer_id,'message':message,'attachment':attachment,'forward_messages':forward_messages,'v':'5.71','access_token':token})
    print((r.content).decode('utf-8'))

S
sgaynetdinov, 2018-08-24
@sgaynetdinov

You should pay attention to the py-vkontakte library , which allows you to work with the vk.com API.

>>> import vk
>>> api = vk.Api(TOKEN)
>>> group = api.get_group('team')  # vk.com/team
>>> group.send_messages(user_id, message='Hello')

Example, bot for VK ( https://github.com/sgaynetdinov/instasave_bot/ )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question