3

Is it possible to send a simple text in the body of a StringRequest using DELETE-Method?

I couldn't find any example where somebody put something in the body of a request... This is my request and I want to add "{'deviceid':'xyz'}" to the body (method is DELETE):

final StringRequest stringRequest = new StringRequest(method, url + "?token=" + token, new Response.Listener<String>() {
        @Override
        public void onResponse(String jsonResponse) {
            // do something
    }, new Response.ErrorListener() {
            // do something
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("api-version", "1");

            return headers;
        }
    };
Unknown User
  • 326
  • 4
  • 14
  • Write own request and `override` `getBody()` and `getBodyContentType()` methods. – dieter_h Sep 02 '15 at 15:35
  • If your server side is Asp.Net WebAPI, IMO, you should read [this question](http://stackoverflow.com/questions/25783542/reason-behind-get-delete-cannot-have-body-in-webapi) and [another question](http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request). Perhaps other webservice is the same – BNK Sep 03 '15 at 03:06
  • Thanks for both answers! @dieter_h Could you post a quick code example please? – Unknown User Sep 03 '15 at 09:06
  • 1
    Now I'm at work. Later i will post complete answer. – dieter_h Sep 03 '15 at 10:57
  • I posted an answer but it's only a copy of this answer I already did: http://stackoverflow.com/questions/23220695/google-volley-how-to-send-a-post-request-with-json-data/31638943#31638943 (if you use the code I wrote there, you'll be able to manage your problem) – Laurent Meyer Sep 03 '15 at 11:26
  • Please read https://github.com/ngocchung/DeleteRequest if you still want to find a working solution :) – BNK Nov 09 '15 at 06:45

3 Answers3

3

Try this:

public class StringJSONBodyReqest extends StringRequest {

    private static final String TAG = StringJSONBodyReqest.class.getName();
    private final String mContent;

    public StringJSONBodyReqest(int method, String url, String content, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
        mContent = content;
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("api-version", "1");

        return headers;
    }


    @Override
    public byte[] getBody() throws AuthFailureError {

        byte[] body = new byte[0];
        try {
            body = mContent.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "Unable to gets bytes from JSON", e.fillInStackTrace());
        }
        return body;
    }


    @Override
    public String getBodyContentType() {
        return "application/json";
    }
}

mContent is your json String

dieter_h
  • 2,619
  • 1
  • 8
  • 17
  • Thank you for this answer, but the body is still being ignored. If I use DELETE in Postman everything seems to work, so my server is fine. Any other idea on how to make Volley send the body? – Unknown User Sep 05 '15 at 12:20
  • I don't hear about any other methods. – dieter_h Sep 05 '15 at 12:36
3

This because Volley doesn't send the Body for DELETE by default. Only for POST, PUT and PATCH. Unfortunate to say the least

There is a workaround for it listed here: Volley - how to send DELETE request parameters?

Community
  • 1
  • 1
Chris
  • 4,076
  • 1
  • 16
  • 11
0
StringRequest stringRequest = new StringRequest(StringRequest.Method.PUT,
            BASE_URL + "/addItem",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                //handle error
                }
            }) {
        @Override
        public byte[] getBody(){
            String jsonString = json to send;
            return jsonString.getBytes();
        }
        @Override
        public String getBodyContentType() {
            return "application/json";
        }
    };
    MyRequestQueue.getInstance().addRequest(stringRequest);
Vijay Patidar
  • 321
  • 3
  • 6