Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python请求不将我的帖子数据作为内容类型发送:multipart/form data auto_Python_Python Requests - Fatal编程技术网

python请求不将我的帖子数据作为内容类型发送:multipart/form data auto

python请求不将我的帖子数据作为内容类型发送:multipart/form data auto,python,python-requests,Python,Python Requests,我编写了一个脚本,它将在文件夹中搜索所有图像,并从excel文件中获取所有信息,然后将这些数据发布到API 我已经做了所有我遇到的困难,python请求假设发送我的POST数据自动内容类型:多部分/表单数据,但它没有作为我的POST数据发送到dict对象脚本将请求作为内容类型:application/x-www-form-urlencoded 导入请求 导入日志记录 导入操作系统 导入json 导入csv 尝试: 将http.client作为http\u客户端导入 除恐怖外: #Python 2

我编写了一个脚本,它将在文件夹中搜索所有图像,并从excel文件中获取所有信息,然后将这些数据发布到API

我已经做了所有我遇到的困难,python请求假设发送我的POST数据自动
内容类型:多部分/表单数据
,但它没有作为我的POST数据发送到dict对象脚本将请求作为
内容类型:application/x-www-form-urlencoded

导入请求
导入日志记录
导入操作系统
导入json
导入csv
尝试:
将http.client作为http\u客户端导入
除恐怖外:
#Python 2
将httplib作为http_客户端导入
http_client.HTTPConnection.debuglevel=1
#必须初始化日志记录,否则将看不到调试输出。
logging.basicConfig()文件
logging.getLogger().setLevel(logging.DEBUG)
requests\u log=logging.getLogger(“requests.packages.urllib3”)
请求\u log.setLevel(logging.DEBUG)
请求\u log.propagate=True
url_和=”https://api.test.com/api/3.0/listings/"
android_令牌='8fas8d7f9a8s7d9f87as9d8f7sa9df7s9f'
标题={
“授权”:“令牌”+android_令牌,
'平台':'安卓',
}
数据_android={}
以open(“listingData1.csv”、“r”)作为f_输入:
csv_输入=csv.DictReader(f_输入)
对于csv_输入中的行:
数据_={
“邮寄详细信息”:第['shipping']行,
‘abcoupay’:‘false’,
“价格”:行[“价格”],
“说明”:行['desc'],
“标题”:行[“标题”],
“meetup”:“false”,
“条件”:“2”,
'邮寄':'真实',
“集合id”:行['cat']
}
搜索路径=行['image']
URL=[]
对于os.listdir(os.getcwd()+搜索路径)中的文件:
如果文件.endswith((“.jpg”、“.jpeg”、“.png”、“.jpg”、“.jpeg”、“.png”):
x=os.getcwd()+“\\”+搜索路径+“\\”+文件
url.append(x)
files={'photo_u%s'%x:('photo_%s.jpg'%x,打开(文件'rb'),'image/jpeg')用于x,文件位于枚举(URL)}
response=requests.request(“POST”,url\u和,data=data\u,headers=headers)
打印(响应.文本.编码(“utf-8”))
python请求发送http请求,如

POST /api/3.0/listings/ HTTP/1.1
Host: api.test.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.21.0
platform: android
Authorization: Token as8fa7d87af7s9d8f79as8df0
Content-Length: 986
Content-Type: application/x-www-form-urlencoded

meetup=false&testupay=false&description=%95+Product+Details%0A%95+Suitable+for+very+small+dogs+and+cats%2C+rabbits+and+other+rodent+carriers+such+as+guinea+pigs%2C+ferrets%2C+rats+and+chinchillas.+Made+of+sturdy+plastic%2C+it+is+sturdy+and+convenient.+A+suitable+grill+allows+air+to+circulate+on+both+sides.+The+door+is+made+Of+plastic+coated+steel.+Thanks+to+the+ergonomic+handle%2C+the+mobile+utility.+It+has+a+mix+of+colors+to+choose+from.+--------%0A%95+80cm+length+X+56cm+width+X+65cm+height%0A%95+Approx.+4.1kg+after+assembly%0A%95+Suitable+for+pets+below+16kg%0A%95+Ideal+for+pet+transportation%0A%95+PP+plastic+material%3B+durable+and+sturdy%0A%95+Great+ventilation+and+air+flow%0A%95+Smart+lock+system%0A%95+Can+be+dismantled+for+easy+storage%0A%95+%2A+Please+check+that+this+item+meets+your+airline%27s+specific+requirements+prior+to+air+travel+usage+%2A%0A&mailing_details=FREE&collection_id=45&price=190&title=Pet+Cat+Dog+Carrier+Portable+Breathable&mailing=true&condition=2'

问题是在发出POST请求时使用的是数据而不是文件

如果您像我在下面所做的那样将请求更改为使用文件字段,则请求应将内容类型设置为multipart/form data

response = requests.request("POST", url_and, files=data_android, headers=headers)

重复:我遵循相同的步骤。你能告诉我代码中的错误是什么吗?我在请求返回会话的第60行得到了一个类似如下的错误:
ile“cookies.py”,第68行,在response=requests.request(“POST”,url\u和,file=data\u,headers=headers)文件“C:\python27\lib\site packages\requests\api.py”。请求(方法=method,url=url,**kwargs)类型错误:request()获取了一个意外的关键字参数“file”
我犯了一个错误,参数名应该是“files”而不是“file”。如果你更新,它应该会工作。