1

I'm trying to do a simple GET request in Android via Volley. However when I debug it the override method(s) are only called AFTER the request has been added to the queue. So my request thinks the headers object is empty and fails on the backend check. How can I make the request consume my headers before being sent to the backend?? I've looked at examples everywhere and I can't figure out why mine doesn't work. Please help!

private void getHello(){
    String helloUrl = "https://...";
    final String basicAuth = "Basic " + Base64.encodeToString("testUser:somePassword".getBytes(), Base64.NO_WRAP);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, helloUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("Hello Response: ",response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Hello Response Error: ",error.toString());
        }
    }){
        @Override
        public Map<String,String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            params.put("X-FXF-clientid", "123");
            params.put("authorization", basicAuth);
            return params;
        }
    };
    queue.add(stringRequest);
}
menix
  • 168
  • 1
  • 1
  • 7
  • What exactly is the backend complaining about? An excerpt from the error log would be great! – karllindmark Jul 28 '20 at 22:25
  • Hey, it says the header object is NULL. Which sort of makes sense if the override is not getting called before the request is added to the queue, right? – menix Jul 29 '20 at 13:44
  • Not necessarily - is the header object one of the specific ones listed in your snippet or all headers? – karllindmark Jul 29 '20 at 13:51
  • It kinda does make sense that `getHeaders(...)` is called sometime after being added to the queue - my expectation would be that the request has yet to be executed (=headers haven't been picked up yet). Does that make sense? – karllindmark Jul 29 '20 at 13:56
  • 1
    Hey, yeah I hear ya. So turns out the backend was reading the headers incorrectly. So it's working now! thanks for helping – menix Jul 29 '20 at 16:23

0 Answers0