0

I want to make a GET request using Volley in Android Studio and in this request I want to include a body. The problem is that when using the GET request, the body in the server is None (I am using Flask). On the other side, when using the POST method, the body reaches the server correctly. This is my code:

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = Globals.WINGS_VEHICLES_URL;

        JSONObject payload = new JSONObject();
        try {
            payload.put("user_id", Globals.USER_ID);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestBody = payload.toString();
        System.out.println(requestBody);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public String getBodyContentType() {
                return "application/json";
            }

            @Override
            public byte[] getBody() {
                return requestBody.getBytes();
            }


        };

        stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);

Is this a Volley restriction, or am I doing something wrong? Assume that I am using a JSONObject as body, non empty.

Edric
  • 18,215
  • 11
  • 68
  • 81
Orestis Zekai
  • 839
  • 1
  • 12
  • 23
  • GET request doesn't allow a body to be sent. If you are using GET you should rather send it as url parameters. – p.mathew13 Dec 23 '19 at 17:15

1 Answers1

0

please check this answer
HTTP GET with request body

and Use POST Http method to send Body!!