0

I have a API on android app and it worked for long time. System get updated and now I'm getting "400 - Bad Request" in the same request (without changes). But in Postman or Advanced REST Client works well.

I'm using: Java Android Retrofit 2 OkHTTP GSON

Android app

interface LoginService

@Headers({
        "Content-Type: application/json",
        "Accept: application/json",
        "Content-Length: 61"
})
@POST(Constants.API_LOGIN)
Call<Login> login(@Body JsonObject json);

Login

        JsonObject json = new JsonObject();
        json.addProperty("email", AUTH_USER);
        json.addProperty("password", AUTH_PASS);

        Call<Login> call = new RetrofitConfig().getLoginService().login(json);
        call.enqueue(new Callback<Login>() {
            @Override
            public void onResponse(@NonNull Call<Login> call, @NonNull Response<Login> response) {
                Login login = response.body();
                Log.d(TAG, "RESPONSE: " + response.message());
                String msg = "Autenticado com sucesso.";
            }

            @Override
            public void onFailure(@NonNull Call<Login> call, @NonNull Throwable t) {
                Log.e(TAG, "Error: " + t.getMessage(), t);
                Toast.makeText(context, "Erro ao autenticar com servidor", Toast.LENGTH_SHORT).show();
                call.cancel();
            }

        });

POST Request

D/OkHttp: --> POST https://API/
D/OkHttp: Content-Type: application/json
    Accept: application/json
    Content-Length: 61
D/OkHttp: {"email":"EMAIL","password":"****"}
    --> END POST (60-byte body)

Response

D/OkHttp: <-- 400 https://API (2130ms)
    server: awselb/2.0
    date: Wed, 15 Jan 2020 14:36:31 GMT
    content-type: text/html
    content-length: 138
D/OkHttp: <html>
    <head><title>400 Bad Request</title></head>
    <body bgcolor="white">
    <center><h1>400 Bad Request</h1></center>
    </body>
    </html>
    <-- END HTTP (138-byte body)

Same Request by Advanced REST Client

Request

Accept: application/json
Content-Type: application/json
cookie: COOKIE
content-length: 61
POST /api/login/ HTTP/1.1
Host: HOST
Accept: application/json
Content-Type: application/json
cookie: COOKIE
content-length: 61

 {"email":"EMAIL","password":"***"}

Response

> 200 OK

{
"token": "TOKEN"
}

Someone can help me?

Dominik Wuttke
  • 535
  • 1
  • 4
  • 12
Lucas Correa
  • 87
  • 2
  • 9

2 Answers2

0

I didn't get your point by "System get updated ". If you mean Android OS, maybe this answer can help you

Mohammad
  • 792
  • 1
  • 7
  • 15
0

I found the solution!

I inserted header Authorization in the Retrofit settings and it was working perfectly. When updating Laravel to version 6.0, when inserting the empty "Authorization" (this api/login was to generate this token) I returned the error "400 - Bad Request".

I just did an if to remove the "Authorization" when there is no token and now it's perfect! \o/

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(HttpLoggingInterceptor.Level.BODY);

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
        .addInterceptor(chain -> {
            Request request;
            if (LoginManager.getToken().length() > 5) {
                request = chain.request().newBuilder()
                        .header("Authorization", LoginManager.getToken())
                        .build();
            } else {
                request = chain.request().newBuilder().build();
            }
            return chain.proceed(request);
        })
Lucas Correa
  • 87
  • 2
  • 9