0

I need to send GET request with body from android application, but it seems that volley ignore it (body). It's always empty on the server side.

I was trying to send body in request and ovverride getBody() - no effect.

In Request.java I saw the comment that getBody only send body for POST or PUT.

Maybe I need to override another method or need to use another library for such task?

My last custom request:

package by.lsd.rmapiconnector

import com.android.volley.NetworkResponse
import com.android.volley.ParseError
import com.android.volley.Response
import com.android.volley.toolbox.HttpHeaderParser
import com.android.volley.toolbox.JsonRequest
import org.json.JSONArray
import org.json.JSONException
import java.io.ByteArrayOutputStream

import java.io.ObjectOutputStream
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset


class CustomJsonRequest(
    method: Int,
    url: String?,
    requestObject: HashMap<String, Any>,
    private val mResponseListener: Response.Listener<JSONArray>,
    errorListener: Response.ErrorListener?
) : JsonRequest<JSONArray>(
    method,
    url,
    requestObject.toString(),
    mResponseListener,
    errorListener
) {

private val mRequestObject = requestObject

override fun deliverResponse(response: JSONArray) {
    mResponseListener.onResponse(response)
}

override fun parseNetworkResponse(response: NetworkResponse): Response<JSONArray> {
    return try {
        val json = String(response.data, Charset.forName(HttpHeaderParser.parseCharset(response.headers)))
        try {
            Response.success(
                JSONArray(json),
                HttpHeaderParser.parseCacheHeaders(response)
            )
        } catch (e: JSONException) {
            Response.error(ParseError(e))
        }
    } catch (e: UnsupportedEncodingException) {
        Response.error(ParseError(e))
    }
}

override fun getHeaders(): Map<String, String> {
    val headers: HashMap<String, String> = HashMap()
    headers["Accept"] = "application/json"
    headers["Content-Type"] = "application/json"
    //headers["Transfer-Encoding"] = "chunked"
    return headers
}

override fun getBody(): ByteArray {
    val byteOut = ByteArrayOutputStream()
    val out = ObjectOutputStream(byteOut)
    out.writeObject(mRequestObject)
    return byteOut.toByteArray()
}

}

  • 2
    Mobile clients can't send a body in a GET request. It's usually not a standard for a GET request to have a body – Ahmad Sattout Oct 29 '21 at 06:46
  • As Ahmad said, you should not send a body in a GET request. For more information, https://stackoverflow.com/questions/978061/http-get-with-request-body – benjiii Oct 29 '21 at 07:06
  • Sure, I saw that post - and it's not prohibited to send body with GET. Target API method on the server needs GET with body, for example in curl or Postman it works. – Сергей Лысёнок Oct 29 '21 at 07:58
  • You will need to use a different way to make the request if Volley doesn't allow you to make a GET request with a body and you can't change the server. While it is not forbidden, a lot of clients or servers will ignore them. – undermark5 Oct 29 '21 at 14:57

0 Answers0