Skip to content Skip to sidebar Skip to footer

How To Make Okhttp Post Request Without A Request Body?

Is there any way to make a post request with OkHTTP that does not have a request body?

Solution 1:

RequestBodyreqbody= RequestBody.create(null, newbyte[0]);  
    Request.BuilderformBody=newRequest.Builder().url(url).method("POST",reqbody).header("Content-Length", "0");
    clientOk.newCall(formBody.build()).enqueue(OkHttpCallBack());

Solution 2:

This worked for me:

RequestBody body = RequestBody.create(null, newbyte[]{});

Solution 3:

  • "".toRequestBody()
  • "".toRequestBody("application/json".toMediaTypeOrNull())

...depends on which media type your endpoint expects.

Solution 4:

I'm using okhttp3.

You can also do this for an empty request body:

val empty: RequestBody = EMPTY_REQUEST

For a POST:

valrequest= Request.Builder().url(http).post(EMPTY_REQUEST).build()

Post a Comment for "How To Make Okhttp Post Request Without A Request Body?"