亲宝软件园·资讯

展开

Python request post Python request post上传文件常见要点

yoyo008 人气:0

通用用法

但上图的字段名,类型需要根据不同接口填写,如某服务接口:

因而对应的上传代码如下:

# 输出参数:请求响应报文
import requests
request_url = 'https://XXXXX/file-upload'
head = {
    # "Content-Type": "multipart/form-data; boundary=alamofire.boundary.3c7024a080e6a27f", #注意,此处不要写Content-Type
"apiVersion": "v1.0.0",
    "appVersion": "v1.0.0",
    "OS": "iOS",
    "OSVersion": "11.4.1",
    "language": "zh",
    "did": "NTIzOTIxNDYwMzMyM2NjZjk1OGM5NjBmYzNlNzg2OTYtZTkxNzg1MzYzNjA4NGM0Mjg4Njg3MmFhNzExMDE1YTgwMDAyLXIwWUtuK0MrS1Y2eDBteWs3WnhDYmQ2ZnovTT0=",
    "reqSeq": "eb144eed4639d36bb6a7b9aa2a563421",
    "timestamp": "1545789471",
     "userToken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJiaXpUeXBlIjoiTE9BTiIsInVzZXJSb2xlIjoyLCJleHAiOjE1NTQ2MTYwMjUsInVzZXJJZCI6MzAxMzkyNDUxNTc3Mzk3MjQ4LCJjaGVja0NvZGUiOiJiZTk1MDEwZjczN2Q0YTU2ODkxNTYyMDBlNDhhZDEyZCJ9.97Km63wUC6IaoSYE1Db8fAxYoW5N-ZQkbiw4kETN2cQ"
}

fl = open('test.png','rb')
files = {'files': ('test.png', fl, 'application/octet-stream', {'Expires': '0'})} #字段名files 以及类型和application/octet-stream 和抓取到的接口一致
r2 = requests.post(request_url, headers=head, files=files)

print(requests.Request('POST', request_url, headers=head, files=files).prepare().body.decode('ascii')) #可以打印出来真实请求的 字段名 以及类型等信息,如果和抓取接口不一致,调整
print(r2.text)

加载全部内容

相关教程
猜你喜欢
用户评论