Reputation: 59
I am trying to create a post request to send a message on the site. But for some reason it doesn't work. I am on the right page, but for some reason the message is still not being sent
print(f'{login}', lastChat['userSms'])
Jurl = 'https://funpay.com/chat/?node=userId'
seska = session.get(Jurl )
soup = BeautifulSoup(seska.text, 'lxml')
tag1 = soup.find('div', id='live-counters').get('data-orders')
tag3 = soup.find('div', class_='chat chat-float').get('data-bookmarks-tag')
tag2 = soup.find("div", class_='chat chat-float').get('data-tag')
tag4 = soup.find('div', class_='param-item chat-panel hidden').get('data-tag')
session.get(Jurl)
dataMes = soup.findAll('div', class_='message')
print(dataMes[-1])
objects = '[{"type":"orders_counters","id":"5214431","tag":"' + tag1 + '","data":false},' \
'{"type":"chat_node","id":"users-5054677-5214431","tag":"' + tag2 + '",' \
'"data":{"node":"users-5054677-5214431","last_message":' + lastMes + ',"content":"s"}},' \
'{"type":"chat_bookmarks","id":"5214431","tag":"' + tag3 + '","data":false},' \
'{"type":"c-p-u","id":"5054677","tag":"' + tag4 + '","data":false}]'
request = '{"action":"chat_message",' \
'"data":{"node":"users-5054677-5214431",' \
'"last_message":' + lastMes + ',"content":"s"}}'
datas = {
"objects": objects,
"request": request,
"csrf_token": csrf
}
print(datas)
session.post(Jurl, data=datas)
Upvotes: 2
Views: 431
Reputation: 23738
Creating the JSON with concatenated strings is error-prone and most likely it is failing because the JSON won't parse.
For example, if variable lastMes
is a string value then it must be quoted.
request = '{"action":"chat_message",' \
'"data":{"node":"users-5054677-5214431",' \
'"last_message":"' + lastMes + '","content":"s"}}'
# ^ ^
Safer to create a dictionary of the values then use json.dumps()
to convert the objects to correctly formatted JSON.
import json
tag1 = tag2 = tag3 = tag4 = lastMes = "XXX"
csrf = "csrf"
objects = [{"type": "orders_counters", "id": "5214431", "tag": tag1, "data": False},
{"type": "chat_node", "id": "users-5054677-5214431", "tag": tag2,
"data": {"node": "users-5054677-5214431",
"last_message": lastMes, "content": "s"}},
{"type": "chat_bookmarks", "id": "5214431", "tag": tag3, "data": False},
{"type": "c-p-u", "id": "5054677", "tag": tag4, "data": False}]
request = {"action": "chat_message",
"data": {"node": "users-5054677-5214431",
"last_message": lastMes,
"content": "s"}}
datas = {
"objects": json.dumps(objects),
"request": json.dumps(request),
"csrf_token": csrf
}
Then make the HTTP POST and check r.status_code
.
r = session.post(Jurl, data=datas)
print(r.status_code)
However, if the web service requires a JSON payload (with Content-type header=application/json) not form-encoded data then you need to use the json= parameter.
datas = {
"objects": objects,
"request": request,
"csrf_token": csrf
}
r = session.post(Jurl, json=datas)
Upvotes: 1