通过RESTAPI使用多部分/表单数据上载文件
我正试图上传4个文件,通过Robot框架在restapi调用中用作请求主体。我正在使用图书馆来实现这一点 我想我在设置正确的MIME类型/边界时遇到了一个问题,因为如果我使用pybot运行文件,就会引发以下错误:通过RESTAPI使用多部分/表单数据上载文件,rest,multipartform-data,robotframework,Rest,Multipartform Data,Robotframework,我正试图上传4个文件,通过Robot框架在restapi调用中用作请求主体。我正在使用图书馆来实现这一点 我想我在设置正确的MIME类型/边界时遇到了一个问题,因为如果我使用pybot运行文件,就会引发以下错误: {u'errorMessage': u"Couldn't find MIME boundary: ------Bound0901"} 这是设置MIME边界的正确方法吗 我可以像下面给出的代码示例那样设置自定义MIME边界吗?或者我需要设置web应用程序定义的边界吗 下面是我用来完成此
{u'errorMessage': u"Couldn't find MIME boundary: ------Bound0901"}
Library | RequestsLibrary
*** TestCases ***
1. Login
Create Session | host | http://10.10.20.20
&{headers}= | Create Dictionary | user=scott | password=tiger
${response}= | RequestsLibrary.Get Request | host | /api/login | ${headers}
&{headers} Create Dictionary | contentType=multipart/form-data;boundary=----Bound0901
${file1}= | Get Binary File | File1.au
${file2}= | Get Binary File | File2.crs
${file3}= | Get Binary File | File3.cst
${file4}= | Get Binary File | File4.des
${data} | Create Dictionary | ----Bound0901Detail={"Name":"APIContent1","isAICC": true,"version": "1.1","availableOffline": false}----Bound0901${file1}----Bound0901${file2}----Bound0901${file3}----Bound0901${file4}----Bound0901
${response}= | RequestsLibrary.Post Request | host | /api/contentimport | data=${data} | headers=${headers}
Log ${response.status_code}
Log ${response.json()}
实际上你在尝试两件事
零件名称
作为键,将文件内容
作为值
*** Settings ***
Library RequestsLibrary
*** TestCases ***
1. Login
Create Session host http://10.10.20.20
&{headers}= Create Dictionary user=scott password=tiger
${response}= RequestsLibrary.Get Request host /api/login ${headers}
${fileData1}= Get Binary File File1.au
${fileData2}= Get Binary File File2.crs
${fileData3}= Get Binary File File3.cst
${fileData4}= Get Binary File File4.des
&{fileParts}= Create Dictionary
Set To Dictionary ${fileParts} file1=${fileData1}
Set To Dictionary ${fileParts} file2=${fileData2}
Set To Dictionary ${fileParts} file3=${fileData3}
Set To Dictionary ${fileParts} file4=${fileData4}
${response}= RequestsLibrary.Post Request host /api/contentimport files=${fileParts} headers=${headers}
Log ${response.status_code}
Log ${response.json()}
以多部分方式提交数据
我必须深入研究Python请求库,以找出对此要做什么。
为了在多部分中发送数据,必须在相应的多部分中将内容类型
属性设置为application/json
。
为此,还要设置多部分的filename
属性,输入值必须是一个包含filename
、fileContent
和contentType
的列表
*** Settings ***
Library Collections
Library OperatingSystem
Library RequestsLibrary
*** TestCases ***
Login And Import
Create Session host http://10.10.20.20
&{headers}= Create Dictionary user=scott password=tiger
${response}= RequestsLibrary.Get Request host /api/login ${headers}
&{fileParts}= Create Dictionary
Create Multi Part ${fileParts} file1 File1.au
Create Multi Part ${fileParts} file2 File2.crs
Create Multi Part ${fileParts} file3 File3.cst
Create Multi Part ${fileParts} file4 File4.des
${data}= Set Variable {"Name":"APIContent1", "isAICC": true, "version": "1.1", "availableOffline": false}
Create Multi Part ${fileParts} Detail data.json contentType=application/json content=${data}
${response}= RequestsLibrary.Post Request host /api/contentimport files=${fileParts} headers=${headers}
Log ${response.status_code}
Log ${response.json()}
*** Keywords ***
Create Multi Part
[Arguments] ${addTo} ${partName} ${filePath} ${contentType}=${None} ${content}=${None}
${fileData}= Run Keyword If '''${content}''' != '''${None}''' Set Variable ${content}
... ELSE Get Binary File ${filePath}
${fileDir} ${fileName}= Split Path ${filePath}
${partData}= Create List ${fileName} ${fileData} ${contentType}
Set To Dictionary ${addTo} ${partName}=${partData}
结果如下:
POST http://10.10.20.20/api/contentimport HTTP/1.1
Host: 10.10.20.20
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.13.0
user: scott
password: tiger
Content-Length: 761
Content-Type: multipart/form-data; boundary=363f55556da84a4083532ce822b09259
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file1"; filename="File1.au"
contents of File1
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file2"; filename="File2.crs"
contents of File2
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file3"; filename="File3.cst"
contents of File3
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file4"; filename="File4.des"
contents of File4
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="Detail"; filename="data.json"
Content-Type: application/json
{"Name":"APIContent1", "isAICC": true, "version": "1.1", "availableOffline": false}
--363f55556da84a4083532ce822b09259--
实际上你在尝试两件事
上载多个文件
发送数据/Json以及文件附件
上载多个文件
创建一个字典,每个文件有一个条目,将零件名称
作为键,将文件内容
作为值
*** Settings ***
Library RequestsLibrary
*** TestCases ***
1. Login
Create Session host http://10.10.20.20
&{headers}= Create Dictionary user=scott password=tiger
${response}= RequestsLibrary.Get Request host /api/login ${headers}
${fileData1}= Get Binary File File1.au
${fileData2}= Get Binary File File2.crs
${fileData3}= Get Binary File File3.cst
${fileData4}= Get Binary File File4.des
&{fileParts}= Create Dictionary
Set To Dictionary ${fileParts} file1=${fileData1}
Set To Dictionary ${fileParts} file2=${fileData2}
Set To Dictionary ${fileParts} file3=${fileData3}
Set To Dictionary ${fileParts} file4=${fileData4}
${response}= RequestsLibrary.Post Request host /api/contentimport files=${fileParts} headers=${headers}
Log ${response.status_code}
Log ${response.json()}
以多部分方式提交数据
我必须深入研究Python请求库,以找出对此要做什么。
为了在多部分中发送数据,必须在相应的多部分中将内容类型
属性设置为application/json
。
为此,还要设置多部分的filename
属性,输入值必须是一个包含filename
、fileContent
和contentType
的列表
*** Settings ***
Library Collections
Library OperatingSystem
Library RequestsLibrary
*** TestCases ***
Login And Import
Create Session host http://10.10.20.20
&{headers}= Create Dictionary user=scott password=tiger
${response}= RequestsLibrary.Get Request host /api/login ${headers}
&{fileParts}= Create Dictionary
Create Multi Part ${fileParts} file1 File1.au
Create Multi Part ${fileParts} file2 File2.crs
Create Multi Part ${fileParts} file3 File3.cst
Create Multi Part ${fileParts} file4 File4.des
${data}= Set Variable {"Name":"APIContent1", "isAICC": true, "version": "1.1", "availableOffline": false}
Create Multi Part ${fileParts} Detail data.json contentType=application/json content=${data}
${response}= RequestsLibrary.Post Request host /api/contentimport files=${fileParts} headers=${headers}
Log ${response.status_code}
Log ${response.json()}
*** Keywords ***
Create Multi Part
[Arguments] ${addTo} ${partName} ${filePath} ${contentType}=${None} ${content}=${None}
${fileData}= Run Keyword If '''${content}''' != '''${None}''' Set Variable ${content}
... ELSE Get Binary File ${filePath}
${fileDir} ${fileName}= Split Path ${filePath}
${partData}= Create List ${fileName} ${fileData} ${contentType}
Set To Dictionary ${addTo} ${partName}=${partData}
结果如下:
POST http://10.10.20.20/api/contentimport HTTP/1.1
Host: 10.10.20.20
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.13.0
user: scott
password: tiger
Content-Length: 761
Content-Type: multipart/form-data; boundary=363f55556da84a4083532ce822b09259
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file1"; filename="File1.au"
contents of File1
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file2"; filename="File2.crs"
contents of File2
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file3"; filename="File3.cst"
contents of File3
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="file4"; filename="File4.des"
contents of File4
--363f55556da84a4083532ce822b09259
Content-Disposition: form-data; name="Detail"; filename="data.json"
Content-Type: application/json
{"Name":"APIContent1", "isAICC": true, "version": "1.1", "availableOffline": false}
--363f55556da84a4083532ce822b09259--
多部分示例中的边界集在哪里?多部分示例中的边界集在哪里?