欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python requests实现multipart/form-data 请求

程序员文章站 2022-05-08 09:59:55
...

使用python的requests 发送multipart/form-data 请求实现模拟登录

设置请求头

headers = {

    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Length": "797",
    # "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarywapmYW7C1gSTBbpK",
    "Cookie": "PHPSESSID=xxxxxxxxxxxxxxxxxxxxxxx",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
}
data={
        "username": "username",
        'nickname': "password",
}

发起post请求、获取返回消息,python3 requests方法中内置json参数。

    res = requests.post(url, json=data, headers=headers)
    print(res)
    print(res.text)

总结

当出现不成功时,发现其他的没什么问题,那就检查下Content-Type,Content-Length删掉或者增加。
相关标签: requests