7

I need to send a video file and JSON object in Rest Assured post call.

Structure is like the following:

{ "sample" : { "name" : "sample-name", "kind" : "upload", "video_file" : multipart file here } }

So I did like the following

Code:

given()
                        .header("Accept", "application/json")
                        .header(auth)
                        .config(rConfig)
                        .body(body)
                        .multiPart("sample[video_file]", new File("path"), "video/mp4")
                        .formParam("sample[name]", "Video Upload")
                        .formParam("sample[kind]", "upload")
                        .log().all().
                        expect()
                        .statusCode(expectedStatusCode)
                        .post(url);

I can't use application/JSON while using multipart in Rest Assured. I explicitly hardcoded the value in the form param and sent the media file in multipart and now it is working fine.

How can I send all the form param data in a single inner object.

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
ARods
  • 441
  • 2
  • 5
  • 13

3 Answers3

2

Your approach is definitely not standard.

You cannot have a multipart request and a JSON body, you need to pick one over the 2 approaches: multipart/form-data or application/json request.

The standard way is to have a multipart request with a "json" param containing the serialized JSON payload, and a "file" param with the multipart file.

given()
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.multiPart(file)
.param("json", "{\"sample\":{\"name\":\"sample- name\",\"kind\":\"upload\",\"video_file\":<this is not needed>}}")

But this involves changing your server-side logic.

If you cannot change your server-side logic, you need to serialize your file as (for instance as an array of bytes, or as base64 string) to be set as video_file in your JSON payload. In which case you'll have an application/json content type request, not a 'multipart/form-data'.

Suraj Gautam
  • 1,524
  • 12
  • 21
Sam
  • 899
  • 6
  • 9
1

You can do this by using RequestSpecBuilder. It supports all the request parameters and you can easily create multipart request.

Sample code taken from https://github.com/rest-assured/rest-assured/wiki/Usage

RequestSpecBuilder builder = new RequestSpecBuilder();
builder.addParam("parameter1", "parameterValue");
builder.addHeader("header1", "headerValue");
RequestSpecification requestSpec = builder.build();

given().
        spec(requestSpec).
        param("parameter2", "paramValue").
when().
        get("/something").
then().
        body("x.y.z", equalTo("something"));
rohit.jaryal
  • 320
  • 1
  • 8
1

Thanks for your response rohit. I was post this question for handling inner object with formParams. I've completed by creating a Hash Map for formParams. Because formParams method of rest assured can accept Hash map.

Form params map creation:

private static Map<String, String> createFormParamsMap(VideoTagInput videoTag) {

        Map<String, String> formParams = new HashMap<>();
        formParams.put(createFormParamKey("name"), "name");
        formParams.put(createFormParamKey("kind"), "kind");

        return formParams;
}

private static String createFormParamKey(String paramKey) {
    return "sample[" + paramKey + "]"; 
    // output is like "sample[name]" - I'm forming inner object here for my purpose.
}

Finally send the map to Rest Assured post call function

given()
                        .header("Accept", "application/json")
                        .header(auth)
                        .config(rConfig)
                        .multiPart("sample[video_file]", new File("path"), "video/mp4")
                        .formParams(requestParamsMap) // requestParamsMap here.
                        .log().all().
                        expect()
                        .statusCode(expectedStatusCode)
                        .post(url);
ARods
  • 441
  • 2
  • 5
  • 13
  • 2
    **NOTE:** Rest assured not allowed to use **.body** and **.multiPart** at the same time. And when we use **.multiPart,** the content-type automatically set to **"multipart/form-data"**. If we want to send JSON we need to set the content-type as **"application/json"**. But "application/json" should not allowing the multipart file to send on the request. So If we want to send some JSON data along with multipart file, Convert the JSON key, values as MAP and send it with **.formParams**, send multipart file in **.multipart** function. – ARods Jan 11 '18 at 02:56