I'm trying to user Android's Volley library to emulate the following cURL request:
curl -H "Content-Type: application/json" -X GET -d '{"password":"password"}' https://0.0.0.0:5000/staging/api/v2.0/supporter/name=Steven
which works as expected, but the following code does not work for android:
public void getSupporterByName(String name, String password, @NonNull final APIRequestCallback<Supporter> callback){
try {
JSONObject job = new JSONObject();
job.put("password", password);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, getAPIEndpoint() + "supporter/name=" + name, job, new Response.Listener<JSONObject>() {
@Override public void onResponse(JSONObject response) {
callback.onSuccess(parseSupporter(response.toString()));
}
}, new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) {
callback.onFailure(error);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> currs = super.getHeaders();
Map<String, String> map = currs != null ? new HashMap<>(super.getHeaders()) : new HashMap<String, String>();
map.put("Content-Type", "application/json");
return map;
}
};
requestQueue.add(req);
} catch (Exception ex) {
callback.onFailure(ex);
}
}
I keep getting a 400 bad request from the API, which I know is because the API isn't seeing the JSONObject that's sent across and therefore not able to validate the password. Is there a way to make a GET
request with a JSON body using Volley?