0
votes

I try to send a DELETE request to my server, but I get a 400 bad request error code back. I search a lot but I can't find any solution that help. When I try it with Postman, the request works fine.

This is how the curl command from Postman looks:

curl --location --request DELETE 'https://blablabla.de' \
--header 'Content-Type: application/json' \
--data-raw '{
"deviceId":"33",
"factoryReset":"0"
}'

This is my Java code:

    public void unlinkDevice(String deviceId) {
    Log.d(LOG_TAG, "unlinkDevice: " + deviceId);
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("deviceId", deviceId);
        jsonObject.put("factoryReset", 0);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.DELETE, url, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            listener.onDeviceUnlink(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            listener.onError(error);
        }
    }) {

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("Cookie", StartApplication.getCookie());
            return headers;
        }
    };

    queue.addToRequestQueue(request);
}

I also try it without to set the Content-Type in the header, but when I get a 415 error and I also removed the getBodyContentType() method, but this also changed nothing.

Any other ideas that can help ?

1
can you confirm the factoryReset is a integer on String? if its a string and you are passing integer that may be causing the issue.Furqan Khan
on the server its handle as a String, but also when I change "factoryReset", 0 to "factoryReset","0" I get a 400 backdevelopKinberg
does your postman runs fine? by changing the factoryReset to integerFurqan Khan
yes this works also via PostmandevelopKinberg
can you confirm the endpoint you are trying to hit is same for the app as well as for the postman.Furqan Khan

1 Answers

0
votes

Okay I found the reason... in volley the body is ignored when you use a DELETE request