Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android volley How to send request with both authentification headers and Json object in body

I need to send via volley a http request with both authentification header and Json object in body. Bu I did not found a request for this in volley.

I found GsonRequest and JsonObjectRequest. GsonRequest int method, String url, Class clazz, Map headers, Listener listener, ErrorListener errorListener, Gson useGson)

JsonObjectRequest (int method, java.lang.String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener)

Any idea what to do ?

like image 205
maxxxo Avatar asked Dec 25 '22 17:12

maxxxo


2 Answers

In your Request class, override getHeaders() to send custom Headers

To send parameters in request body you need to override either getParams() or getBody() method of the request classes

described here:

Asynchronous HTTP Requests in Android Using Volley

like image 124
Androiderson Avatar answered Dec 31 '22 14:12

Androiderson


try this code

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> params = new HashMap<String, String>();
    String creds = String.format("%s:%s","USERNAME","PASSWORD");
    String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
    params.put("Authorization", auth);
    return params;
}
like image 45
Mina Fawzy Avatar answered Dec 31 '22 14:12

Mina Fawzy