0

I want to make a POST request to a server with retrofit, but I always get error 422. I just wanna know if I do something wrong or it is the server fault.

Retrofit call:

OkHttpClient.Builder duplicateTimeSessionClient = new kHttpClient.Builder();

duplicateTimeSessionClient.addInterceptor(newInterceptor() {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
                                    .header("Authorization", "Bearer "+sharedPreferences.getString("access_token",""))
                                    .header("Accept", "application/vnd.web_app+json; version=1")
                                    .method(original.method(), original.body())
                                    .build();

        return chain.proceed(request);
    }
});

OkHttpClient client = duplicateTimeSessionClient.build();

final Retrofit duplicateTimeSession = new Retrofit.Builder()
                            .baseUrl(context.getString(R.string.URL))
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .addConverterFactory(GsonConverterFactory.create())
                            .client(client)
                            .build();

duplicateService = duplicateTimeSession.create(DuplicateTimeSessionInterface.class);                   

Call<String> duplicateTimeSessionCall = duplicateService.duplicate(createJSON(pos),"api/time-sessions");

duplicateTimeSessionCall.enqueue(new Callback<String>() {
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        Log.i("time-session post",Integer.toString(response.code()));
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {}
});

Interface:

public interface DuplicateTimeSessionInterface {
    @POST
    Call<String>duplicate(@Body JSONObject jsonObject, @Url String url);
}

EDIT:

The same JSON works from the browser or iPhone, and some similar calls work from Android, this one just doesn't want to go through.

f-CJ
  • 4,235
  • 2
  • 30
  • 28
Boanta Ionut
  • 402
  • 8
  • 24
  • Does the same thing happen without Retrofit (ie, with PostMan)? Code 422 indicates possible logic errors in the content you are sending, and the server cannot deal with it. – john16384 Mar 21 '17 at 11:16
  • I didn't try with PostMan, but similar POST requests work, it is just a particular category(on a specific branch ex. time sessions) that gives this error. I will try with PostMan. – Boanta Ionut Mar 21 '17 at 11:18
  • Try using a POJO class instead of createJSON(pos) – Rissmon Suresh Mar 21 '17 at 11:20
  • I tried that too, also tried to take the exact JSON the browser is sending, still 422. – Boanta Ionut Mar 21 '17 at 11:22

0 Answers0