vutil provides a REST API to execute multipart requests. This API handles the multipart request and response as well. This API provides options to configure your multipart request and process the multipart response as well.

API Endpoint:

API Endpoint for executing multipart request/process response is 

POST {{vutilBaseURL}}/request

Here 

             * You may also specify "default" as key and value will be the processor name.

Let us take some examples on, how you can execute multipart requests with the help of vutil API. Then, later on, we will cover, how we can handle the multipart response.

First create a test case which will execute the request API of the vutil server. This is a POST API.  

Now, we will set the multipart data to be sent in the request body of this test case. We will cover the multiple scenarios of setting multipart data to this API.

Example 1: Executing multipart/form-data request. Suppose I want to submit a form having some attached files, then I can configure the request body like this.

The actual request body of this POST request will look like this:

----------------------------967739397936203919086726
Content-Disposition: form-data; name="param1"
 
value1
----------------------------967739397936203919086726
Content-Disposition: form-data; name="param2"
 
value2
----------------------------967739397936203919086726
Content-Disposition: form-data; name="file1"; filename="file1-data.json"
Content-Type: application/json
 
Contents of file1-data.json
----------------------------967739397936203919086726
Content-Disposition: form-data; name="file2"; filename="custom-file-name.xml"
Content-Type: application/xml
 
Contents of file2-data.xml
----------------------------967739397936203919086726
Content-Disposition: form-data; name="files-array"; filename="sample1.json"
Content-Type: application/json
 
Contents of sample1.json
----------------------------967739397936203919086726
Content-Disposition: form-data; name="files-array"; filename="sample2.png"
Content-Type: application/png
 
Contents of sample2.png
----------------------------967739397936203919086726--

Example 2: Executing multipart request. Suppose I want to send multipart data, then I can configure the request body like this.

The actual request body for this POST request will look like this:

----------------------------967739397936203919086726
Content-Type: application/json
 
Contents of file1-data.json
----------------------------967739397936203919086726
Content-Type: application/xml
 
Contents of file2-data.xml
----------------------------967739397936203919086726--

Example 3: Executing nested multipart/related request. Suppose I want to send nested multipart body, then I can configure the request body like this.
 
In the below screenshot, we are sending two parts. The first part is a simple JSON file and the second part is again a multipart body. So we define the second part's body as an array of parts.

----------------------------967739397936203919086726
Content-Type: application/json
 
Contents of file1-data.json
----------------------------967739397936203919086726
Content-Type: multipart/related; boundary=638305241626696072121975;
transfer-encoding: chunked
 
----------------------------638305241626696072121975
Content-Type: application/xml
 
Contents of file2-data.xml
----------------------------638305241626696072121975
Content-Type: application/png

Contents of sample2.png
----------------------------638305241626696072121975--
----------------------------967739397936203919086726--

Handling of API response:

We will cover different examples of handling of API responses.

Example 1: Suppose API response is a normal JSON object.

If no responseOptions are set in the request body then vutil will automatically parse the JSON response and will provide the following response:

{
  "body": {
    "hello": "world" //Sample JSON Response
  },
  "headers": {
    "content-type": "application/json",
    "date": "Tue, 02 May 2017 19:16:27 GMT",
    "connection": "close"
  },
  "statusCode": 200
}

If you don't want to parse the API response then you may set the parse option to false in responseOptions object like below:

{
	"responseOptions": {
		"parse": false
	}
}

then the API response will be as follows:

{
  "body": "{\"hello\": \"world\"}", //JSON Response in string form
  "headers": {
    "content-type": "application/json",
    "date": "Tue, 02 May 2017 19:16:27 GMT",
    "connection": "close"
  },
  "statusCode": 200
}

Example 2: Suppose API response is a multipart response.

Now suppose, the API returns a multipart response. The response has three parts, one part contains JSON response, the second part contains XML response and the third part contains the PNG file. Now, how you will validate such response in vREST.

To process such type of API response, you may need to process the API response first before it is read by vREST. Simply set the responseOptions like below:

{
	"responseOptions": {
		"parse": true,
		"process": {
			"json": "json", //json processor simply parses the JSON response
			"xml": "xml2json", //xml processor converts xml into JSON 
			"png": "checksum" //checksum processor calculates the md5 checksum of the part data
		}
	}
}

And the API response might look like:

{
  "body": {
    "parts": [
      { //first part response
        "body": {
			"hello": "world"
		},
        "bodylen": 20,
        "headers": {
          "content-type": "application/json"
        }
      },
      { //second part response
        "body": {
          "iterations": {
            "iteration": {
              "request": {
                "body": {
                  "name": {},
                  "email": {
                    "#text": "john.doe@example.com"
                  },
                  "designation": {
                    "#text": "Chief Technical Officer"
                  },
                  "organization": {
                    "#text": "example.com"
                  },
                  "country": {
                    "#text": "India"
                  },
                  "aboutMe": {
                    "#text": "Please write something about me…"
                  },
                  "twitterId": {
                    "#text": "fake.john.doe"
                  },
                  "facebookId": {
                    "#text": "fake.john.doe"
                  },
                  "githubId": {
                    "#text": "fake.john.doe"
                  },
                  "createdOn": {
                    "#text": "2014-05-03T06:28:45.479Z"
                  }
                }
              }
            }
          }
        },
        "bodylen": 630,
        "headers": {
          "content-disposition": "form-data; name=\"file2\"; filename=\"small.xml\"",
          "content-type": "application/xml"
        }
      },
      { //third part response
      	"body": "63c3998d81a7bdbac448b4c76d4d08f3", //md5 checksum of the png file
        "bodylen": 97955,
        "headers": {
        	"content-type": "application/png"
        }
      }
    ]
  },
  "headers": { //response headers
    "content-type": "multipart/form-data; boundary=--------------------------566038963922264557922999",
    "date": "Tue, 02 May 2017 12:27:11 GMT",
    "connection": "close",
    "transfer-encoding": "chunked"
  },
  "statusCode": 200 //API response status code
}


Example 3: Suppose API response is a spreadsheet file (xlsx / xls).

Now suppose, the API returns a spreadsheet file as a response. To process such type of API response, you may need to process the API response first before it is read by vREST. Simply set the responseOptions like below:

{
	...
	"responseOptions": {
		"parse": true,
		"process": {
			"default": "xls2json", //xls2json processor converts xlsx or xls into JSON
		}
	}
}

And the API response might look like:

{
  "body": [
	{
	  "key": "key1",
	  "value": "value1"
 	},
    {
	  "key": "key2",
	  "value": "value2"
	}
  ],
  "headers": { //response headers
    "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "date": "Fri, 17 May 2019 11:32:29 GMT",
    "connection": "close"
  },
  "statusCode": 200 //API response status code
}