Uploading file to S3 using Rest Assured multipart.

Rest AssuredDynamic ProgrammingProgramming

We can upload files to S3 using Rest Assured multipart with the help of the below techniques −

  • Rest Assured has the default URL encoding feature. The issue with S3 URLs is that they contain special characters such as %2A, %3D. As the URL encoding feature is configured to true value by default in Rest Assured, we are required to set it to false so that the special characters are not transformed to ASCII equivalent value during runtime.

    Syntax −

    given().urlEncodingEnabled(false)

  • Rest Assured appends a default charset to the content. This causes a problem if the content type is not given. In some scenarios, we might be required to mention the content type in the header. Simultaneously, Rest Assured appends the default charset to the content type while sending a request. This causes unexpected behavior. To get rid of this, we need to set a false value to the class EncoderConfig.

    Syntax −

    given().config(RestAssured.config().encoderConfig(encoderConfig().append DefaultContentCharsetToContentTypeIfUndefined(false)))

Sample Code Implementation

given().
//setting urlEncoding to false
urlEncodingEnabled(false).

//upload file is of JSON type
header("Content-type", "application/json").

//setting class EncoderConfig to false
config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).
body(Files.readAllBytes(Paths.get(payLoad.json))).
when().
put("endpoint").
then().
log().all().and()
.assertThat().statusCode(200);
raja
Updated on 17-Nov-2021 11:32:43

Advertisements