This tutorial is going to cover about how to post with OkHttp, an HTTP & HTTP/2 client for Android and Java applications. We’re going to get though some popular use cases and examples such as how to post a string, how to post a form, how to post a stream and so on.

1. Preparation

We’re going to use OkHttp, version 3.5.0 for all OkHttp post examples in the tutorial, and the only Maven dependency we will need is:

2. Post a String with OkHttp

Let’s see a following example which we will post a string with OkHttp using OkHttpClient.

In the example, after creating the client, we create a request by providing the builder the URL of the remote endpoint, the HTTP Post method and the request body. Note how we created the request body by providing the text we want to post and its media type.

Because the entire request body is in memory simultaneously, we should avoid posting large (greater than 1 MiB) documents using this API.

3. Post a Byte Array

It’s easy to post a byte array with OkHttp. Following is an example that we will post a byte array to a RESTful web service.

4. Post Form Parameters

OkHttp allows us to post form parameters by using FormBody.Builder to build a request body that works like an HTML <form> tag. Names and values will be encoded using an HTML-compatible form URL encoding. Let’s see an example which we will post form parameters with OkHttp using OkHttpClient.

In the example, we have just used the FormBody.builder to create a form with 3 fields: email, firstname, lastname and then posted the form to the remote URL.

5. Post a File

It’s pretty simple to post a file with OkHttp. We can use the RequestBody class to create a request body that transmits the content of a file. Let’s see the following example which we will post a file using OkHttpClient:

In the above example, we have posted the Lorem Ipsum.txt file to a RESTful web service.

6. Post a Stream

With OkHttp, we can POST a request body as a stream. The content of this request body is being generated as it’s being written. Let’s see an example which we will post a stream to a REST API.

In the example, we posted a stream by creating new RequestBody and overriding the writeTo method in which we read the file Lorem Ipsum.txt line by line and streamed into the buffered sink of the request.

7. Post a Multipart Request

We can use the MultipartBody.Builder class to build request bodies compatible with HML file upload forms. We can leverage this feature to upload files, images with OkHttp. Let’s see an example which we will post a multipart request to a RESTful web service.

In the example, we created an instance of the MultipartBody class which is a an RFC 2387-compliant request. Then we added a file part and a description part to the request body, and finally we execute the request to post the form to the RESTful web service.

8. Conclusion

The tutorial has showed us how to post with OkHttp in different ways and use cases from how to post a string, how to post a stream, how to post a Multipart request and so on.

The OkHttp post examples source code can be found on the Github project. It’s an Maven based project, so it’s easy to import and run it in any IDE such as Eclipse, Intellij, etc.

Below are other related tutorials for your references:

How to Cache Response with OkHttp

Java REST Client Examples Using OkHttp

Basic Authentication with OkHttp Example

Upload a File with OkHttp

Set Timeout with OkHttp

Download a File with OkHttp

WebSocket Client Example with OkHttp

 

0 0 vote
Article Rating