snowzkr 发表于 2021-7-7 19:05:53

Python的requests求助!

请问各位大神下面这种请求用requests怎么写啊?试了几种写法都是报错{:10_266:}

Method
POST

HTTP header
Content-Type: multipart/form-data;boundary=“boundary”

HTTP body
—boundary
Content-Disposition: form-data
Content-Type: application/json

{“service_id”: string,
“product_id”: string,
“info”: { “corpo_name”: string,
               “address”: string }
}

—boundary
Content-Disposition: form-data; name=“inputfile”
Content-Type: application/pdf

(binary data)


尝试了以下写法都报错
方法1
headers = {“Content-Type”: ‘ multipart/form-data;boundary=“boundary”’}
payload = {“service_id”: string, “product_id”: string,“info”: { “corpo_name”: string,“address”: string }}
files = {inputfile: (filename, open(filename,”rb”))}
res = requests.post(URL, json=payload, files=files, headers=headers)

方法2
headers = {“Content-Type”: ‘ multipart/form-data;boundary=“boundary”’}
files = {inputfile: (filename, open(filename,”rb”)),
“service_id”: (None,string), “product_id”: (None,string) ,“info”: { “corpo_name”: (None,string),“address”: (None,string)}}
res=requests.post(URL,files=files,headers=headers)

wp231957 发表于 2021-7-7 19:15:30

还是拿具体网站说话吧
这么说看起来乱乱的

snowzkr 发表于 2021-7-8 16:24:50

不好意思 网站不太好公开requests里需要post 一段json和一个文件… 如何同时post呢…

redforce 发表于 2021-7-8 21:19:09

header = {"Content-Type": "multipart/form-data"}
file = {'inputfile': open('test.pdf', 'rb')}
requests.post(url,json=payload,files=file,headers=header)
如上

redforce 发表于 2021-7-8 21:30:01

file = {'inpufile': ('test.pdf', open('test.pdf', 'rb'), 'application/pdf)}
设置content-type的写法如上。两种都试一下

骚年¥学编程 发表于 2021-7-9 09:25:23

感觉看着像是 一个是请求头的信息 一个是请求体的信息所以都放在请求头里 是有问题的   如果网站不方便放出来 就截个图把关键信息打码一下要不然不好分析
页: [1]
查看完整版本: Python的requests求助!