Reputation: 11
Need help with this. I'm trying to upload a zip file executing a POST request to a REST API. I'm using robotframework along with request library. After see some example I getting to nowhere. Here is my code:
${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
I want to execute the same request as this 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"
Upvotes: 0
Views: 3513
Reputation: 11
After some rechearch I was able to find a solution for uploading a zip file. First thing: not need to set content type in the header because the request library do the job for you. Second: for the file to upload you need to create the dictionary with some specific configuration because along with the data you need to provide as well Content-Disposition: form-data; name="file"; filename="Lampadaires.zip" Content-Type: application/x-zip-compressed in my case. So using Get Binary File keyword is not enough.
This was my code in the end
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}
Upvotes: 1