Retrofit: JSON String Escaping Problem in Multipart

Created on 6 Jan 2016  路  1Comment  路  Source: square/retrofit

I use Retrofit 2.0.0-beta2 and 2.0.0-beta3.

If I POST data to my Backend the JSON String will escaped with backslashes and starts/ends with an ".

@Multipart
@POST("upload")
Call<JSONObject> upload(@PartMap Map<String,RequestBody> files,
    @Part("version") String version,
    @Part("data") String data // <-- JSON String
);

My JSON String...

{"foo":"kit","bar":"kat"}

... will transformed after POST with retrofit to

"{\"foo\":\"kit\",\"bar\":\"kat\"}"

In my backend I have to remove all backslashes and the first/last Character. Then I can use the JSON_decode function in PHP.

If I use @FormUrlEncoded instead of Multipart without sending any files everything works fine, but I need to POST files.

Is there any solution? Retrofit should POST the RAW JSON String.

Most helpful comment

You need to either:

  • Use RequestBody for a raw value
  • Add the ScalarsConverterFactory (from the converters-scalars module) before the JSON converter factory on your Retrofit.Builder for String support.

Right now the String is being passed through your JSON converter and being escaped into a JSON string.

>All comments

You need to either:

  • Use RequestBody for a raw value
  • Add the ScalarsConverterFactory (from the converters-scalars module) before the JSON converter factory on your Retrofit.Builder for String support.

Right now the String is being passed through your JSON converter and being escaped into a JSON string.

Was this page helpful?
0 / 5 - 0 ratings