Woohoo ! You have made a great choice for the library you are using to automate your REST API’s. All you need is a bit more understanding of how it works behind the scenes. In case you are struggling with calling a REST API that uploads files to your cloud storage, for e.g. Amazon S3 using Java + Rest-Assured, the following things might be of help:-
1) Rest Assured has in built in URL encoding(documentation link) – The problem with S3 URLs is that they have these special characters like %2A, %3D etc and since Rest Assured has the URL encoding set to true by default, you would need to set it off so that they are not converted into their Ascii equivalents at runtime:-
2) Rest Assured adds a default charset to your content which becomes problematic when the content type is not specified explicitly. In some cases, we might need to give the content type in the header, and rest assured automatically adds its default charset to the content type while sending the request which leads to unexpected results. In order to turn this setting off, we can use the EncoderConfig class as follows:-
Summing it all up, your rest-assured java code to upload files on S3 should look something like(in my case, the file was a jpg so the content type is image/*):-
given(). urlEncodingEnabled(false). header("Content-Type","image/*"). config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))). body(Files.readAllBytes(Paths.get(filePath))). when(). put(URI). then(). log().all().and() .assertThat().statusCode(HttpStatus.SC_OK);
Happy (REST) Automating!
Categories: Common Issues