Uploading files using multipart/form-data through REST API -


i'm trying upload 4 files used request body in rest api call through robot framework. i'm using requests library achieve this.

i think i'm facing problem setting correct mime type/boundary error thrown if run file using pybot:

{u'errormessage': u"couldn't find mime boundary: ------bound0901"} 
  1. is correct way set mime boundary?
  2. can set custom mime boundaries, have in code sample given below? or required set boundaries defined web app?

here's code i'm using done:

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}=  |  binary file  |  file1.au     ${file2}=  |  binary file  |  file2.crs     ${file3}=  |  binary file  |  file3.cst     ${file4}=  |  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()} 

actually trying 2 things,

  1. uploading multiple files
  2. sending data/json along file attachments

to upload multiple files

create dictionary 1 entry per file, part name key , file content value.

*** 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}=  binary file  file1.au   ${filedata2}=  binary file  file2.crs   ${filedata3}=  binary file  file3.cst   ${filedata4}=  binary file  file4.des    &{fileparts}=  create dictionary   set dictionary  ${fileparts}  file1=${filedata1}   set dictionary  ${fileparts}  file2=${filedata2}   set dictionary  ${fileparts}  file3=${filedata3}   set dictionary  ${fileparts}  file4=${filedata4}    ${response}=  requestslibrary.post request  host  /api/contentimport  files=${fileparts}  headers=${headers}    log  ${response.status_code}   log  ${response.json()} 

to submit data in multipart

i had dig through python requests library figure out this. sending data in multipart, content-type attribute has set application/json in respective multipart. that, , set filename attribute of multipart, entry value has list filename, filecontent , contenttype.

*** settings *** library  collections library  operatingsystem library  requestslibrary  *** testcases *** login , 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  binary file  ${filepath}   ${filedir}  ${filename}=  split path  ${filepath}   ${partdata}=  create list  ${filename}  ${filedata}  ${contenttype}   set dictionary  ${addto}  ${partname}=${partdata} 

resulting in following:

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-- 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -