首页 > 解决方案 > 请求发布字典格式错误的数据

问题描述

将发送到服务器的表单数据如下所示: 表单数据

我无法将其正确发布到服务器。我试过这样:

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 错误。

如果有人可以帮助我,那就太好了。

此致!

标签: pythonpython-requestsmechanize

解决方案


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

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

还是谢谢你们的帮助。


推荐阅读