Retrofit: Query parameters not getting marshaled according to the specified converter

Created on 5 Jun 2014  路  1Comment  路  Source: square/retrofit

in the documentation, it is said under section RestAdapter Configuration

Retrofit uses Gson by default to convert HTTP bodies to and from JSON

This seems not be happening when a date is passed as query parameter, while providing a Gson with a custom date format.

For example

Service:

public interface MyService {
  @GET("/api/birthdate")
  List<Date> getBirthDates(@QueryParam("from") Date fromDate, 
         @QueryParam("to") Date toDate);
}

Adapter configuration:

GsonConverter converter = new GsonConverter(new GsonBuilder()
                // use ISO 8601 formatted date
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                .create());
final RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint("http://api.example.com")
                .setConverter(converter)
                .setLogLevel(LogLevel.FULL)
                .build()
MyService service = adapter.create(MyService.class);
// trying to load birthdates from Jan 1, 1970 to now
service.getBirthDates(new Date(0), new Date());

this will generate a url as follows:

http://api.example.com/api/birthdate?from=Thu+Jan+01+05:30:00+GMT+00:00+1970
&to=Thu+Jun+05+19:37:23+GMT+00:00+2014

but according to the documentation, the date sent in request should've been marshalled and produced an url as follows:

http://api.example.com/api/birthdate?from=1970-01-01T00:00:00+0000
&to=2014-05-19T19:37:23+0000

Although this maybe circumvent manually using a DateFormat and changing the signature of the service.

Most helpful comment

Query params are not part of the HTTP body and therefore do not go through the converter. Additionally, query params have no concept of length of mime type which the converter uses.

From the @Query javadoc:

Values are converted to strings using String.valueOf(Object) and then URL encoded. A List or array will result in a query parameter for each value. null as a value to the parameter or as a value in a list will not be included in the URL.

>All comments

Query params are not part of the HTTP body and therefore do not go through the converter. Additionally, query params have no concept of length of mime type which the converter uses.

From the @Query javadoc:

Values are converted to strings using String.valueOf(Object) and then URL encoded. A List or array will result in a query parameter for each value. null as a value to the parameter or as a value in a list will not be included in the URL.

Was this page helpful?
0 / 5 - 0 ratings