Fuel

bintray Build Status Codecov

The easiest HTTP networking library for Kotlin/Android.

You are looking at the documentation for 2.x.y.. If you are looking for the documentation for 1.x.y, checkout the 1.16.0 README.md

Features

  • HTTP GET/POST/PUT/DELETE/HEAD/PATCH requests in a fluent style interface
  • Asynchronous and blocking requests
  • Download as a file
  • Upload files, Blobs, DataParts as multipart/form-data
  • Cancel asynchronous request
  • Debug logging / convert to cUrl call
  • Deserialization into POJO / POKO
  • Requests as coroutines
  • API Routing

Installation

We offer maven and jitpack installations. Maven via bintray only has stable releases but jitpack can be used to build any branch, commit and version.

Maven

You can download and install Fuel with Maven and Gradle. The core package has the following dependencies:

  • Kotlin - Kotlin
  • Result - 2.2.0
  1. //core
  2. implementation 'com.github.kittinunf.fuel:fuel:<latest-version>'
  3.  
  4. //packages
  5. implementation 'com.github.kittinunf.fuel:<package>:<latest-version>'

Make sure to include jcenter() in your repositories

  1. repositories {
  2. jcenter()
  3. }

Each of the extensions / integrations has to be installed separately.

PackageDescription
fuelCore package
fuel-androidAndroid: Automatically invoke handler on Main Thread when using Android Module
fuel-coroutinesKotlinX: Execution with coroutines
fuel-forgeDeserialization: Forge
fuel-gson(De)serialization: Gson
fuel-jacksonDeserialization: Jackson
fuel-jsonDeserialization: Json
fuel-kotlinx-serialization(De)serialization: KotlinX Serialization
fuel-livedataAndroid Architectures: Responses as LiveData
fuel-moshiDeserialization: Moshi
fuel-reactorReactive Programming: Responses as Mono (Project Reactor 3.x)
fuel-rxjavaReactive Programming: Responses as Single (RxJava 2.x)
fuel-stethoUtility: Debug utility for Android on Chrome Developer Tools, Stetho

Jitpack

If you want a SNAPSHOT distribution, you can use Jitpack

  1. repositories {
  2. maven(url = "https://www.jitpack.io") {
  3. name = "jitpack"
  4. }
  5. }
  6.  
  7. dependencies {
  8. //core
  9. implementation(group = "com.github.kittinunf.fuel", name = "fuel", version = "-SNAPSHOT")
  10.  
  11. //packages
  12. // replace <package> with the package name e.g. fuel-coroutines
  13. implementation(group = "com.github.kittinunf.fuel", name = "<package>", version = "-SNAPSHOT")
  14. }

or

  1. dependencies {
  2. //core and/or packages
  3. // replace <package> with the package name e.g. fuel-coroutines
  4. listof("fuel", "<package>").forEach {
  5. implementation(group = "com.github.kittinunf.fuel", name = it, version = "-SNAPSHOT")
  6. }
  7. }

Configuration

  • group is made up of com.github as well as username and project name

  • name is the subproject, this may be any of the packages listed in the installation instructionseg. fuel, fuel-coroutines, fuel-kotlinx-serialization, etc

  • version can be the latest master-SMAPSHOT or -SNAPSHOT which always points at the HEAD or any other branch, tag or commit hash, e.g. as listed on jitpack.io.

We recommend not using SNAPSHOT builds, but a specific commit in a specific branch (like a commit on master), because your build will then be stable.

Build time-out

Have patience when updating the version of fuel or building for the first time as jitpack will build it, and this may cause the request to jitpack to time out. Wait a few minutes and try again (or check the status on jitpack).

NOTE: do not forget to add the kotlinx repository when using coroutines or serialization

Forks

Jitpack.io also allows to build from fuel forks. If a fork's username is $yourname,

Quick start

Fuel requests can be made on the Fuel namespace object, any FuelManager or using one of the String extension methods. If you specify a callback the call is async, if you don't it's blocking.

Async Usage Example

  1. import com.github.kittinunf.fuel.httpGet
  2. import com.github.kittinunf.result.Result;
  3.  
  4. fun main(args: Array<String>) {
  5.  
  6. val httpAsync = "https://httpbin.org/get"
  7. .httpGet()
  8. .responseString { request, response, result ->
  9. when (result) {
  10. is Result.Failure -> {
  11. val ex = result.getException()
  12. println(ex)
  13. }
  14. is Result.Success -> {
  15. val data = result.get()
  16. println(data)
  17. }
  18. }
  19. }
  20.  
  21. httpAsync.join()
  22. }

Blocking Usage Example

  1. import com.github.kittinunf.fuel.httpGet
  2. import com.github.kittinunf.result.Result;
  3.  
  4. fun main(args: Array<String>) {
  5.  
  6. val (request, response, result) = "https://httpbin.org/get"
  7. .httpGet()
  8. .responseString()
  9.  
  10. when (result) {
  11. is Result.Failure -> {
  12. val ex = result.getException()
  13. println(ex)
  14. }
  15. is Result.Success -> {
  16. val data = result.get()
  17. println(data)
  18. }
  19. }
  20.  
  21. }
  22.  
  23. // You can also use Fuel.get("https://httpbin.org/get").responseString { ... }
  24. // You can also use FuelManager.instance.get("...").responseString { ... }

Fuel and the extension methods use the FuelManager.instance under the hood. You can use this FuelManager to change the default behaviour of all requests:

  1. FuelManager.instance.basePath = "https://httpbin.org"
  2.  
  3. "/get"
  4. .httpGet()
  5. .responseString { request, response, result -> /*...*/ }
  6. // This is a GET request to "https://httpbin.org/get"

Detailed usage

Check each of the packages documentations or the Wiki for more features, usages and examples. Are you looking for basic usage on how to set headers, authentication, request bodies and more? fuel: Basic usage is all you need.

Basic functionality

Responses

(De)serialization

Utility

Other libraries

If you like Fuel, you might also like other libraries of mine;

  • Result - The modelling for success/failure of operations in Kotlin
  • Fuse - A simple generic LRU memory/disk cache for Android written in Kotlin
  • Forge - Functional style JSON parsing written in Kotlin
  • ReactiveAndroid - Reactive events and properties with RxJava for Android SDK

Credits

Fuel is brought to you by contributors.

Licenses

Fuel is released under the MIT license.