Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robot Framework “multipart/form-data” REST request with multiple parameters

I am trying to use requestslibrary to upload some files, goal is to achieve this:

------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="files"; filename="photo-2.jpeg"
Content-Type: image/jpeg


------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="fileId"

b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*
------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="extract"

false
------WebKitFormBoundary61N9vqJ7380nh6iv--

and now I have this, as per this:

  ${data}=    Evaluate    {'files': open("C:/testautomation/resources/Assets/photo-2.jpeg", 'r+b'), 'extract': (None, 'false'), 'fileId': (None, 'b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*')}
  log  ${data}
  ${result}=  Post Request  rest  ${url}  headers=${HEADERS}  files=${data}

I THINK that the only bit I am missing is the "Content-Type: image/jpeg" from the first part, but how on earth I can add that? Currently the file gets uploaded, but it is not considered to be an image file.

like image 967
JiiSal Avatar asked Apr 27 '26 23:04

JiiSal


1 Answers

The answer was:

${data}=  Evaluate  {'files': ('photo-1.jpeg', open("C:/testautomation-robot/resources/Assets/photo-1.jpeg", 'r+b'), 'image/jpeg'), 'extract': (None, 'false'), 'fileId': (None, 'b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*')}

Found an example from here: https://code.i-harness.com/en/q/bcfb9b

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
In the above, the tuple is composed as follows:

(filename, data, content_type, headers)
like image 142
JiiSal Avatar answered Apr 30 '26 05:04

JiiSal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!