Gotcha: URL Encoding for consecutive double slashes issue with Rest Assured

Featured image for sharing metadata for article

Earlier today I was writing some Java to interact with a RESTful API using Rest Assured. However, I was seeing some weird results with URL encoded characters in my perfectly reasonable URLs.

The code I was using was similar to below:

// passed into the method as an empty string
String argument = "";
String FORMAT = "https://some/api/%s/";
given()
  .when()
  .log().all()
  .get(String.format(FORMAT, argument))
  .then()
  .log().all();

When running this, I received the following logs from Rest Assured:

Request method:	GET
Request URI:	https://some/api%2F%2F/
Proxy:			<none>
Request params:	<none>
Query params:	<none>
Form params:	<none>
Path params:	<none>
Headers:		Accept=*/*
Cookies:		<none>
Multiparts:		<none>
Body:			<none>

Notice that the URL has two %2Fs, which are the URL encoded value of a /.

I didn't expect this, and after doing some searching around found two issues on the Rest Assured issue tracker: issue 335 and issue 867.

It appears that the workaround is to disable URL encoding:

 String argument = "";
 String FORMAT = "https://some/api/%s/";
 given()
+  .urlEncodingEnabled(false)
   .when()
   .log().all()
   .get(String.format(FORMAT, argument))
   .then()
   .log().all();

Or:

 String argument = "";
 String FORMAT = "https://some/api/%s/";
 given()
   .when()
+  .urlEncodingEnabled(false)
   .log().all()
   .get(String.format(FORMAT, argument))
   .then()
   .log().all();

Which gives gives a correct request:

Request method:	GET
Request URI:	https://some/api///
Proxy:			<none>
Request params:	<none>
Query params:	<none>
Form params:	<none>
Path params:	<none>
Headers:		Accept=*/*
Cookies:		<none>
Multiparts:		<none>
Body:			<none>

This appears to affect at least RestAssured versions v2.8.0 and v3.3.0.

Note: RestAssured appears to add a trailing slash, leading to three slashes.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #java #rest-assured.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.