Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post multipart file and JSON in Rest Assured

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.

like image 294
ARods Avatar asked Jan 08 '18 13:01

ARods


People also ask

How do you send a multipart file in request body in Rest assured?

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.


1 Answers

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"));
like image 166
rohit.jaryal Avatar answered Sep 24 '22 07:09

rohit.jaryal