Adilp
Adilp

Reputation: 459

Volley authentication header

Currently when I try to make a get request, I have to authenticate first. So looking on the web it seems I need to use the getHeaders() and use a hashmap to submit my credentials. However my header has a little extra info I need to send out.

My header looks like this: enter image description here

I'm not sure what the problem could be. I feel like im sending all the right info.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlJsonObj, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                   // JSONObject jsonArray = response.getJSONObject("response_message");

                    String message = response.getString("response_message");

                    Toast.makeText(getActivity(),
                                    message,
                                   Toast.LENGTH_LONG).show();


                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }

            }


         ,

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley", "Error");
                    }
                }


        ) {
            @Override
            public Map getHeaders() throws AuthFailureError {
                Map headers = new HashMap<>();
                String credentials = "ezhu:Ccare@123";
                String auth = "Basic "
                        + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                headers.put("Content-Type", "application/json");
                headers.put("Authorization", auth);
                headers.put("user-tz", "-330");


                return headers;
            }
        };



        requestQue.add(jsonObjectRequest);

    }

I get a 401 error in my log and it gives me the URL. When I click the URL a tab in my browser opens and shows the following:

{"error_message":"Missing request method parameter","error_code":"1","error":"Missing request header 'user-tz' for method parameter of type String"}

Upvotes: 1

Views: 2394

Answers (1)

Albert Mosiałek
Albert Mosiałek

Reputation: 190

Your code seems to be correct. You can try to find something in your logs. When I was dealing similar issue, I found

W/System: Ignoring header sessionID because its value was null.

in my logs, what was pretty helpful

Upvotes: 1

Related Questions