请求发布数据时出现字典格式错误

时间:2019-10-04 23:16:15

标签: python python-requests mechanize

将要发送到服务器的formdata如下所示: Formdata

我没有将其正确发布到服务器。我已经这样尝试过了:

payload = {
    "ad_id": ad_id,
    "ad_type": "1",
    "csrf_token": crsf_token,
    "messages": [{'content': 'message to server', 'message_type': 'text'}],
    "user_id": "9020446"
}
#
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
    "Content-Type": "application/json",
    "Origin": "https://www.wg-gesucht.de",
    "Referer": f"https://www.wg-gesucht.de/nachricht-senden.html?message_ad_id={ad_id}"
}
res = browser.post('https://www.wg-gesucht.de/ajax/api/Smp/api.php?action=conversations', payload, headers=headers)

我也这样尝试过:

"messages": ['message to server']

但是我总是遇到401错误。

如果有人可以帮助我,那将是很棒的事情。

最诚挚的问候!

2 个答案:

答案 0 :(得分:0)

您可以在python中使用“请求”库

import requests
payload = {
    "ad_id": ad_id,
    "ad_type": "1",
    "csrf_token": crsf_token,
    "messages": [{'content': 'message to server', 'message_type': 'text'}],
    "user_id": "9020446"
}
#
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
    "Content-Type": "application/json",
    "Origin": "https://www.wg-gesucht.de",
    "Referer": f"https://www.wg-gesucht.de/nachricht-senden.html?message_ad_id={ad_id}"
}
res = requests.post('https://www.wg-gesucht.de/ajax/api/Smp/api.php?action=conversations', data=payload, headers=headers)

答案 1 :(得分:0)

我刚刚将posttype更改为“ json”,这为我修复了错误:

res = browser.post('https://www.wg-gesucht.de/ajax/api/Smp/api.php?action=conversations', json=payload, headers=headers)

仍然感谢您的帮助。