Forum Discussion

New2API's avatar
New2API
Frequent Contributor
7 years ago
Solved

How to post a REST with Json value and base64 encoded file as a multipart/form-data?

Experts I need help with posting a file with Content-Type: multipart/form-data header.

I need to send some metadata information in Json format. File should encoded in base64 format.

 

what have I done so far:

  • Used attachment tab - but it encodes file in Binary format by default
  • I have encoded a file and used post QueryString option to send metadata and file. but somehow it fails to append file name to the "Content-Disposition" header inside multipart boundary.

for Ex: below is the expected body for my post request: (Tested in fiddler and it works!!! encoded file has been truncated below.)

 

------=_Part_16_244158447.1548867718200
Content-Disposition: form-data; name="documentset"
Content-Type: application/json; charset=utf-8

 

{"locations":{"contact":["ABC"],"relationship":["23456"]},"metadata":{"categories":[{"category":"Documents","subcategory":"Governing Documents"}],"date":"2019-01-30","id":"45351","name":"Test Document - Added by QA Automation"},"properties":{"application":"ABC","username":"QAUSER"}}

------=_Part_16_244158447.1548867718200
Content-Disposition: form-data; name="document0"; filename="TestDoc.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: base64

JVBERi0xLjQNJeLjz9MNCjI4IDAgb2JqDTw8L0UgNzg4OTkvSCBbIDQxOSAxNDUgXS9MIDU4NzA4
MC9MaW5lYXJpemVkIDEvTiA2L08gMzAvVCA1ODY0MTg+Pg1lbmRvYmoNICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICANeHJlZg0yOCA3DTAwMDAwMDAwMTYgMDAwMDAgbg0KMDAwMDAwMDM1OCAw
MDAwMCBuDQowMDAwMDAwNTY0IDAwMDAwIG4NCjAwMDAwMDA3MjQgMDAwMDAgbg0KMDAwMDAwMDc1
NiAwMDAwMCBuDQowMDAwMDAwODU4IDAwMDAwIG4NCjAwMDAwMDA0MTkgMDAwMDAgbg0KdHJhaWxl
cg08PC9JbmZvIDIyIDAgUi9QcmV2IDU4NjQwOS9Sb290IDI5IDAgUi9TaXplIDM1Pj5zdGFydHhy
ZWYNIDANJSVFT0YNDQ0NDTI5IDAgb2JqDTw8L1BhZ2VzIDIxIDAgUi9UeXBlIC9DYXRhbG9nL1Zl
cnNpb24gLzEuND4+DWVuZG9iag0zNCAwIG9iag08PC9GaWx0ZXIgL0ZsYXRlRGVjb2RlL0xlbmd0

------=_Part_16_244158447.1548867718200--

 

My setup in soapUI is captured in soapuirequest.png 

when running the request, body generated looks like below:

 


------=_Part_16_244158447.1548867718200
Content-Disposition: form-data; name="documentset"

 

Content-Type: application/json; charset=utf-8

 

{"locations":{"contact":["ABC"],"relationship":["23456"]},"metadata":{"categories":[{"category":"Documents","subcategory":"Governing Documents"}],"date":"2019-01-30","id":"45351","name":"Test Document - Added by QA Automation"},"properties":{"application":"ABC","username":"QAUSER"}}
------=_Part_16_244158447.1548867718200
Content-Disposition: form-data; name="document0"

 

;filename="TestDoc.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: base64

JVBERi0xLjQNJeLjz9MNCjI4IDAgb2JqDTw8L0UgNzg4OTkvSCBbIDQxOSAxNDUgXS9MIDU4NzA4
MC9MaW5lYXJpemVkIDEvTiA2L08gMzAvVCA1ODY0MTg+Pg1lbmRvYmoNICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICANeHJlZg0yOCA3DTAwMDAwMDAwMTYgMDAwMDAgbg0KMDAwMDAwMDM1OCAw
MDAwMCBuDQowMDAwMDAwNTY0IDAwMDAwIG4NCjAwMDAwMDA3MjQgMDAwMDAgbg0KMDAwMDAwMDc1
NiAwMDAwMCBuDQowMDAwMDAwODU4IDAwMDAwIG4NCjAwMDAwMDA0MTkgMDAwMDAgbg0KdHJhaWxl
cg08PC9JbmZvIDIyIDAgUi9QcmV2IDU4NjQwOS9Sb290IDI5IDAgUi9TaXplIDM1Pj5zdGFydHhy
ZWYNIDANJSVFT0YNDQ0NDTI5IDAgb2JqDTw8L1BhZ2VzIDIxIDAgUi9UeXBlIC9DYXRhbG9nL1Zl

------=_Part_16_244158447.1548867718200

 

here is the issue:

  1. in the body for json part - Headers have a line space as shown below 

    ------=_Part_16_244158447.1548867718200
    Content-Disposition: form-data; name="documentset"

     

    Content-Type: application/json; charset=utf-8

  2. file part headers are also not correct due to filename in the next line

                  ------=_Part_16_244158447.1548867718200
                  Content-Disposition: form-data; name="document0"

 

                  ;filename="TestDoc.pdf"
                  Content-Type: application/pdf
                  Content-Transfer-Encoding: base64

 

when executing this request how to introdue/inject those headers in red?

 

any help is greatly appreciated.

thanks!