Reputation: 1387
I am trying to create the Android version of a curl GET request. Works great in terminal, but fails in the app.
The curl request (urls are not real):
curl -d '{"detail": "Y"}' -H "Content-Type:application/json" -X GET -H "Auth-Token:longstringhereforauthtoken" https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/
It gives back what I need without any issue.
Well, now I want to use that request in a Volley request in Android, however, I am getting back:
Unexpected response code 406 for https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/
The code where I'm attempting the request is as follows (will show relevant variables that are set beforehand):
private String url;
private String request;
private String searchRequest;
String selected = getIntent().getStringExtra("node");
String token = getIntent().getStringExtra("token");
url = getResources().getString(R.string.api_url);
request = url + "/Session/";
searchRequest = url + "/GSLB/"+selected+"/"+selected+"/";
ProgressDialog searchDialog = new ProgressDialog(MainActivity.this);
searchDialog.setMessage("Getting zone info...");
searchDialog.show();
JSONObject object = new JSONObject();
try {
object.put("Content-Type", "application/json");
object.put("Auth-Token", token);
object.put("detail", "Y");
} catch(JSONException e) {
Log.d(TAG, e.getMessage());
}
Log.d(TAG, "Selected "+icon+", Token: "+token);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, searchRequest, object,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
searchDialog.dismiss();
Log.d(TAG, String.valueOf(response));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
searchDialog.dismiss();
if( error instanceof NetworkError) {
Log.d(TAG, error.toString());
Log.d(TAG, "Request to "+searchRequest+" FAILED...");
} else if( error instanceof ServerError) {
Log.d(TAG, "Request to "+searchRequest+" FAILED...");
Log.d(TAG, error.toString());
} else if( error instanceof AuthFailureError) {
Log.d(TAG, error.toString());
Log.d(TAG, "Request to "+searchRequest+" FAILED...");
} else if( error instanceof ParseError) {
Log.d(TAG, error.toString());
Log.d(TAG, "Request to "+searchRequest+" FAILED...");
} else if( error instanceof NoConnectionError) {
Log.d(TAG, error.toString());
Log.d(TAG, "Request to "+searchRequest+" FAILED...");
} else if( error instanceof TimeoutError) {
Log.d(TAG, error.toString());
Log.d(TAG, "Request to "+searchRequest+" FAILED...");
}
}
});
RequestQueue searchRequestQueue = Volley.newRequestQueue(MainActivity.this);
searchRequestQueue.add(jsonObjectRequest);
That is when I receive the error:
BasicNetwork.performRequest: Unexpected response code 406 for https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/
Before this activity, I have a LoginActivity that does a similar request, which is where the token comes from in the GET request. It sends credentials and received the Auth-Token. This one works, so I am unsure of why the previous snippet is failing.
The Login request:
JSONObject object = new JSONObject();
try {
object.put("user_name", usernameEditText.getText().toString());
object.put("customer_name", customernameEditText.getText().toString());
object.put("password", passwordEditText.getText().toString());
object.put("Content-Type", "application/json");
} catch(JSONException e) {
Log.d(TAG, e.getMessage());
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, request, object,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if(response != null) {
Log.d("JSON", String.valueOf(response));
String token = null;
pDialog.dismiss();
try {
token = response.getJSONObject("data").getString("token");
Log.d("JSON", token);
} catch (JSONException e) {
Toast.makeText(LoginActivity.this, "" + e.getMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Intent loginIntent = new Intent(LoginActivity.this, SelectActivity.class);
loginIntent.putExtra("token", token);
LoginActivity.this.startActivity(loginIntent);
} else {
Toast nullToast = Toast.makeText(LoginActivity.this, "Invalid Credentials\nPlease try again", Toast.LENGTH_LONG);
nullToast.show();
usernameEditText.getText().clear();
customernameEditText.getText().clear();
passwordEditText.getText().clear();
}
}
}
I have done these requests in both PHP and cURL, but can't seem to understand why it's failing with Android. I feel like my syntax is correct, but perhaps I'm missing something?
Upvotes: 1
Views: 486
Reputation: 8713
406 http error code stays for "not acceptable". This means that your request has an incorrect header. I believe that JsonObjectRequest
doesn't manage headers, but the request body.
In order to work on request header you need to override the getHeaders() method from VolleyRequest class, something like:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Auth-Token", token);
params.put("detail", "Y");
return params;
}
Upvotes: 0
Reputation: 1387
The clue was in the request response error code (406). Instead of this:
JSONObject object = new JSONObject();
try {
object.put("Content-Type", "application/json");
object.put("Auth-Token", token);
object.put("detail", "Y");
} catch(JSONException e) {
Log.d(TAG, e.getMessage());
}
I used the getHeaders()
method to send my headers, like so:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Auth-Token", token);
params.put("detail", "Y");
return params;
}
Though I do not understand why my object.put()
does not work (It was in any tutorials I viewed, so I need to research more), this was what fixed it. I'm now getting my response successfully.
Upvotes: 1