Android pass Authentication Token on header using Volley

0

I'm having trouble sending authentication token to the header using volley

This is my code

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Log.i("onActivityCreated: ", "token: ====> " + deliverymanEntitie.getTokenEntitie().getToken());

    RequestQueue queue = Volley.newRequestQueue(getContext());
    String url = RunfoxService.URL_GET_ALL_ORDERS_TO(deliverymanEntitie.getId());
    StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.i("onResponse: ", response);
        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            try {
                String json = new String(error.networkResponse.data, "UTF-8");
                Log.e("onErrorResponse: ", json);
                Log.e("onErrorResponse: ", error.networkResponse.headers.toString());


            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-type", "application/application/json; charset=UTF-8");
            headers.put("Accept", "application/json; charset=UTF-8");
            headers.put("api_token", deliverymanEntitie.getTokenEntitie().getToken());

            Log.i("getHeaders: ", headers.toString());

            return headers;
        }
    };

    queue.add(stringRequest);

}

In the code above we have all the requests made, here are my logs

My api error log

E/onErrorResponse:: {"message":"Token de acesso n\u00e3o informado"}

Testing with Postman the same request and also invalidating the token header I can get the json result correctly.

Apparently I can not tell the token in the headers, can anyone tell me where I'm wrong?

    
asked by anonymous 19.03.2018 / 22:59

1 answer

1

I solved my problem by simply removing the "_" from api_token like this:

headers.put("apitoken", deliverymanEntitie.getTokenEntitie().getToken());

This way I got to pass and receive in my API, I do not know about the reason but it worked for me.

    
21.03.2018 / 15:13