I have the following Body sent via postman return just fine (as raw JSON)
{"payload":{"email":"","username":"","password":""}}
However when i try to send it via Retrofit as follows, it doesn't work (gives 500):
public Observable<JsonObject> signup(String email, String userName, String password) {
JsonObject data = new JsonObject();
JsonObject payload = new JsonObject();
data.addProperty("email", email);
data.addProperty("username", userName);
data.addProperty("password", password);
payload.add("payload", data);
return mShoutApiService.signup(payload);
}
Service call and adapter are as follows
service:
@POST("/signup")
Observable<JsonObject> signup(@Body JsonObject payload);
adapter:
private RestAdapter createRestAdapter(String hostUrl, GsonConverter gsonConverter, OkClient okClient) {
return new RestAdapter.Builder()
.setClient(okClient)
.setConverter(gsonConverter)
.setEndpoint(hostUrl)
.setLogLevel(LogLevel.FULL)
.setLog(new AndroidLog("SHOUT_LOG"))
.build();
}
private GsonConverter getGsonConverter(Gson gson) {
return new GsonConverter(gson);
}
private OkClient getOkClient() {
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS);
return new OkClient(okHttpClient);
}
Logs show as follows
03-23 05:04:00.380 2442-3468/shout.surge.com.shout D/SHOUT_LOG: ---> HTTP POST http://shout-api.herokuapp.com/signup
03-23 05:04:00.380 2442-3468/shout.surge.com.shout D/SHOUT_LOG: Content-Type: application/json; charset=UTF-8
03-23 05:04:00.380 2442-3468/shout.surge.com.shout D/SHOUT_LOG: Content-Length: 52
03-23 05:04:00.384 2442-3468/shout.surge.com.shout D/SHOUT_LOG: {"payload":{"email":"","username":"","password":""}}
03-23 05:04:00.384 2442-3468/shout.surge.com.shout D/SHOUT_LOG: ---> END HTTP (52-byte body)
03-23 05:04:09.180 2442-3468/shout.surge.com.shout D/SHOUT_LOG: <--- HTTP 500 http://shout-api.herokuapp.com/signup (8796ms)
It appears they are the exact same call but for some reason it fails in my mobile app. any insight as to what I'm missing would be great!
I took a look at your issue and as I suspected it's indeed related with a small difference in the calls made in the app with the calls made from postman. Unfortunately I cannot explain to you why this happens because being a 500 it's most likely a faulty implementation on the server side.
The issue got solved for me when I added the header Accept: application/json
like so:
@Headers("Accept: application/json")
@POST("/signup")
Observable<JsonObject> signup(@Body JsonObject payload);
I've noticed that the tool I'm using (Advanced REST Client for chrome) was adding said header to every call.
When I remove this header I get the 500 you were experiencing. You can also use Accept: */*
, but I'm not sure this is a good practise.
I'm sorry I don't have enough to tell you why this happens, but it fixed the issue for me. I did try it in an android app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With