3

I am using Node.js at the Backend, and need to send some params(parameters) and a jwt token(for authorization) in header of a GET request. I am using express-jwt module at the node server. The request need to be authorized using jwt token there. Simply sending the token as params do not work there.

    private void getInfo(final String instanceId, final String token) {
//token is jwt token
            StringRequest strReq = new StringRequest(Request.Method.GET,
                    Config.URL_GET_ID_INFO, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response);

                    try {
                        JSONObject responseObj = new JSONObject(response);
                        boolean error = responseObj.getBoolean("error");
                        String message = responseObj.getString("message");
                        if (!error) {

                            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + message,
                                    Toast.LENGTH_LONG).show();
                        }

                        // hiding the progress bar
                        //progressBar.setVisibility(View.GONE);

                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();

                       // progressBar.setVisibility(View.GONE);
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    //progressBar.setVisibility(View.GONE);
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String , String>();
                    params.put("instanceId", instanceId);

                    //Need to send jwt along with params, but do not know how to include it in the request.
                    Log.e(TAG, "Posting params: " + params.toString());

                    return params;
                }



            };

            // Adding request to request queue
            MyApplication.getInstance().addToRequestQueue(strReq);
        }

`

shikhar bansal
  • 1,629
  • 2
  • 21
  • 43
  • Are you sure the server isn't expecting the token in your request headers? Sending a token in the body of a request is rather unusual. Typically you will do something like ```RequestObject.addHeader( "auth-token" , token ) ``` – Olivercodes Dec 11 '15 at 19:16
  • And then server side would verify with ```const token = req.headers["token"]; //proceed to process token``` – Olivercodes Dec 11 '15 at 19:17

1 Answers1

4

Just override the Request's method getHeaders()

Here's an example :

@Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Authorization", "Bearer "+ yourToken);
            return params;
        }
khalid karam
  • 112
  • 1
  • 7