简体   繁体   中英

How to create volley header having 'Content-Type', Authorization Token and body?

I am trying to create a login volley post request using custom server with oauth 2.0 using authentication token. Body:

{
"customer": {
    "email": "example@gmail.com",
    "password":"12345"
  }
}

I tried this

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
       final Map<String, String> headers = new HashMap<>();
       headers.put("Content-Type", "application/json");
       headers.put("Authorization", "Bearer .....UzI1NiIsInR5cCI6IkpXVCJ9.....");
       headers.put("email",username.toString());
       headers.put("password",password.toString());
       headers.put("customer",headers.toString());

       Log.e("Json created", headers.toString());
       return headers;
}

Do like this if you want to pass data as JSON body..

String body = "{\"customer\":{\"email\":"+ username + ",\"password\":"+ password +"}}";

StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
    //response here
}, error -> {
    //exceptions here
}) {
    @Override
    public byte[] getBody() throws AuthFailureError {
        return body.getBytes();
    }

    @Override
    public HashMap<String, String> getHeaders() throws AuthFailureError {
        final HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer .....UzI1NiIsInR5cCI6IkpXVCJ9.....");
        return headers;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM