Fuel your HTTP Requests for Kotlinkotlin ·
While we all were busy retrofit-ing our HTTP requests on Android, I stumbled across this amazing and easy to use HTTP library called Fuel!
Let me show you how easy your HTTP requests on Android can be ☺️
I will be covering Fuel + Gson, hence here are the required dependencies for the build.gradle file:
A Simple Get Request with Fuel:
Let’s GET this URL: dummy.restapiexample.com/api/v1/employee/1
Which should print out:
That’s all for GET, now let’s try a POST request.
POST request with Fuel:
Let’s POST this URL: http://httpbin.org/post
Ok, that’s great and all. You and I both write clean code and we pretty much use Model objects that are Serialised for our HTTP POST requests.
Well, if you are like me and if you use Gson for Serialising-Deserialising your Model objects, then let me show you how easy that is as well!
POST request with Gson and Fuel:
A typical Kotlin Model class would look like:
Let’s create an Instance of the Person Object:
Let’s make that POST request!
It’s important to set the “Content-Type” to “application/json” after serialising with Gson, otherwise your server may receive the personJson within another json object.
This saves a whole lot of burden with the setup that other HTTP libraries require. However you may be interested in the Router-pattern and such for your services, which I believe can be setup using Fuel as well.
That’s all for today! I hope this quick blog of mine was helpful to you. Feel free to comment your questions!