Get the Token from a JWT Api

Asked

Viewed 216 times

-1

I am having problems doing the authentication in an api with JWT standard, the token it comes in header and using the retrofit I am not getting it. The consumption of the api in Postman usually occurs the login resource, in the body a json with the e-mail credentials and password, the request returns an empty body, already observing the header we located the information of the token. When implementing the login on android always fall on onFailure ie I’m not sure how to get/read the information that is in the header.

I’m putting my code down first on the interface

  @Headers({"Content-Type: application/json", "Accept: application/json"})
  @POST("login")
  Call<Authorization> login(@Body Credenciais cred);

now where I am consuming, what strikes me is that I know the api it returns me an empty body and the information I want is in the header so when I have a Call to Authorization this in my view would not be correct because the retrofit it will want to parse the return to the object only that in the body comes back nothing and then.... could be that this is the cause of the problem

    private void authenticate(String email, String password) {
    Log.i(TAG, "authenticate()" );

    Credenciais creds = new Credenciais(email, password);

    ApiUtil.getServiceClass().login(creds)
            .enqueue(new Callback<Authorization>() {
                @Override
                public void onResponse(Call<Authorization> call, Response<Authorization> response) {

                    if (response.isSuccessful()) {
                        Log.i(TAG, "Token: " + response.headers().get("Authorization"));
                    } else {
                        Log.i(TAG, "onResponse() - Não foi um sucesso");
                    }
                }

                @Override
                public void onFailure(Call<Authorization> call, Throwable t) {
                    Log.i(TAG, "onFailure() - Opá ocorreu uma falha " + t.getMessage());
                    call.cancel();
                }
            });

inserir a descrição da imagem aqui

1 answer

0

In this case of the JWT pattern where we have an empty body return the problem is in the signature of the way this below is wrong the retrofit it will try to serialize the object

@Headers({"Content-Type: application/json", "Accept: application/json"})
@POST("login")
Call<Authorization> login(@Body Credenciais cred);

To correct we will put void

@Headers({"Content-Type: application/json", "Accept: application/json"})
@POST("login")
Call<Void> login(@Body Credenciais cred);

and to get the Token now stayed like this

ApiUtil.getServiceClass().login(creds)
            .enqueue(new Callback<Void>() {
                @Override
                public void onResponse(Call<Void> call, Response<Void> response) {
                    if (response.isSuccessful()) {
                        Log.i(TAG, "Token: " + response.headers().get("Authorization"));
                    } else {
                        Log.i(TAG, "onResponse() - Não foi um sucesso");
                    }
                }

                @Override
                public void onFailure(Call<Void> call, Throwable t) {
                    Log.i(TAG, "onFailure() - Opá ocorreu uma falha " + t.getMessage());
                    call.cancel();
                }
            });

I hope it will be useful for those with the same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.