需要帮助。我正在尝试将执行POST请求的zip文件上载到REST API。我正在使用robotframework和请求库。看了一些例子之后,我无处可去。这是我的代码:
${DATA}= Get Binary File C:\\Users\\${USERNAME}\\data\\Lampadaires.zip
${DATA}= Get File C:\\Users\\${USERNAME}\\data\\Lampadaires.zip encoding=CP437
&{dictFiles} Create Dictionary file=${DATA} type=application/x-zip-compressed
&{headers} Create Dictionary Content-Type=multipart/form-data accept=*/*
Create Session session http://[test_url]docker.net:8080 headers=${headers}
${resp} Post Request session /rest/v1/organizations/${ORGANIZATION_ID}/upload files=${dictFiles}
Should Be Equal As Strings ${resp.status_code} 200
我想执行与此CURL相同的请求
curl -X POST "http://[test_url]docker.net:8080/rest/v1/organizations/${ORGANIZATION_ID}/upload" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "file=C:\Users\${USERNAME}\data\Lampadaires.zip;type=application/x-zip-compressed"
经过一些重新搜索后,我找到了上传zip文件的解决方案。第一件事:不需要在标头中设置内容类型,因为请求库为您完成了这项工作。第二:对于要上传的文件,您需要创建具有某些特定配置的字典,因为您还需要提供与数据一起提供的内容。 name =“文件”; filename =“ Lampadaires.zip”内容类型:在我的情况下为application / x-zip-compressed。因此,使用Get Binary File关键字是不够的。
这是我的最终代码
Library RequestsLibrary
Library Collections
Library OperatingSystem
***Variables***
${ORGANIZATION_ID} 1234
${ALIAS} MyAlias
${MCS_URL} http://test_url:8080
${FILE_UPLOAD_PATH} C:\\Users\\Daryll\\Documents\\data\\Lampadaires.zip
***Test Cases***
Zip File Upload
Create Session ${ALIAS} ${MCS_URL}
&{headers}= Create Dictionary Accept=*/*
&{fileParts}= Create Dictionary
Create Multi Part ${fileParts} file ${FILE_UPLOAD_PATH} application/x-
zip-compressed
${response}= Post Request ${ALIAS}
/rest/organizations/${ORGANIZATION_ID}/upload files=${fileParts}
headers=${headers}
Log ${response.status_code} console=${True}
Log ${response.json()} console=${True}
*** Keywords ***
Create Multi Part [Arguments] ${addTo} ${partName} ${filePath}
${contentType} ${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}