cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Contributor

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Moderator

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

Hi,

 

Ok, thank you for the information. I can recommend that you check the following articles:

How to send binary data in the request payload?

Troubleshooting issues with adding attachments to REST requests

 


Join the annual community event and share your stories to win gifts
Wintertainment2019

View solution in original post

6 REPLIES 6
Highlighted
Moderator

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

Hi, 

 

Thank you for your post. I cannot reproduce the issue when there is an empty line between headers, or the filename parameter is on a separate line (I used ReadyAPI 2.6.0).

 

The needed headers should be added if you add a file on the Attachments tab of the Request.

 

To further troubleshoot the issue, I will need to see the content of the TestCase properties and the screenshot of the Attachments tab. 

 

Also, I can recommend that you open a new case here: https://support.smartbear.com/message/?prod=ReadyAPI

 

 

 


Join the annual community event and share your stories to win gifts
Wintertainment2019
Contributor

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

Hi Nastya, Thank you for the effort and reply.

 

I didn't attach file to attachment tab due to default binary encoding. Request is designed to send base64 encoded file. Is there any way to change the default file encoding inside ReadyAPI?

 

Now for the empty line between those multipart headers -  

I am creating json part on the fly via groovy script in order to update json nodes with different data such as currentdate, new relationship no, contact etc... As part of this process I am hardcoding second header line as I am not sure how to include it automatically with form-data.

 

 

//## build json body and write to a file for reference ##//
def REQUEST = new JsonBuilder(metaDataVal).toString()
    REQUEST = "Content-Type: application/json; charset=utf-8\r\n\r\n" + "$REQUEST" 
    
    testRunner.testCase.setPropertyValue("docMetaData", REQUEST)

For the file part - I am encoding the file and hard coding other headers too (attached a file to show the file content). 

 

Multipart/form-data puts only Content-Disposition: header.

 

//## READ canned request from a file to modify ##//
def testFile	 = new File(RequestPath + "testFile.txt").text //## base64 encoded format is stored in this file ##//
    testRunner.testCase.setPropertyValue("testFile", "$testFile")

 

Moderator

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

Hi,

 

Ok, thank you for the information. I can recommend that you check the following articles:

How to send binary data in the request payload?

Troubleshooting issues with adding attachments to REST requests

 


Join the annual community event and share your stories to win gifts
Wintertainment2019

View solution in original post

Contributor

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

Hey Nastya, awesome!!!

Second link helped me to inject or correct the headers in multi-part body of a request. 

 

Thanks a ton!

 

I have not used attachment tab to attach a file.

Instead, I encoded a file using groovy. Groovy script does the following.

  • Reads a canned json from a file and updates json nodes with right attributes
  • Saves updated json to a testcase property
  • Reads a pdf file and encodes in base64 format
  • Saves encoded file to a testcase property

In Rest Step, I am calling above testcase properties as query parameters. Also, selected media type as "Multipart/form-data' and checked "Post QueryString" option.

 

In order to inject required headers I used below event handler script. (RequestFilter.filterRequest)

import org.apache.http.client.methods.HttpRequestBase
import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport

def bodyPart = context.getProperty("httpMethod").requestEntity.message.content.getBodyPart(0) //## get a part by number, it starts with 0
    
    //## Add headers to json section of body
    bodyPart.addHeader("Content-Type", "application/json; charset=utf-8") //## specify the value without the name parameter 

def bodyPart1 = context.getProperty("httpMethod").requestEntity.message.content.getBodyPart(1) // get a part by number, it starts with 0
    bodyPart1.removeHeader("Content-Disposition") //## delete default headder added by SoapUI

    //## Add headers to PDF section of body
    bodyPart1.addHeader("Content-Disposition", "form-data; name="+"document0"+"; filename="+"TesTDoc.pdf")
    bodyPart1.addHeader("Content-Type", "application/pdf") 
    bodyPart1.addHeader("Content-Transfer-Encoding", "base64")

 

Moderator

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

You're welcome! I'm glad to hear that the article helped you.

 

And, thank you for sharing your script with us!

 


Join the annual community event and share your stories to win gifts
Wintertainment2019
Contributor

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

Folks, some update and observation on my approach---

As mentioned earlier, I injected headers related to file through grovy script. 

Including header "Content-Transfer-Encoding" not only adds this header but it encodes the content again. This makes file to corrupt and rejected by the file consumption module.

 

In order to avoid further complexity I used attachment tab to include a pdf file.