Convert Figma logo to code with AI

kittinunf logofuel

The easiest HTTP networking library for Kotlin/Android

4,552
429
4,552
91

Top Related Projects

42,958

A type-safe HTTP client for Android and the JVM

45,699

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries.

🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀

3,376

Quick Overview

Fuel is a lightweight HTTP networking library for Kotlin/Android. It provides a simple and flexible way to make HTTP requests, handle responses, and manage network operations in Kotlin-based applications.

Pros

  • Easy to use and intuitive API
  • Supports both synchronous and asynchronous requests
  • Built-in support for JSON parsing and serialization
  • Extensible architecture with plugins for additional functionality

Cons

  • Limited documentation and examples compared to more established libraries
  • May lack some advanced features found in larger networking libraries
  • Primarily focused on Kotlin, which might be a limitation for projects using multiple languages

Code Examples

  1. Making a simple GET request:
import com.github.kittinunf.fuel.httpGet

val (request, response, result) = "https://api.example.com/data".httpGet().responseString()
println(result)
  1. Performing a POST request with parameters:
import com.github.kittinunf.fuel.httpPost
import com.github.kittinunf.fuel.core.extensions.jsonBody

val json = """{"username": "john", "password": "secret"}"""
val (request, response, result) = "https://api.example.com/login"
    .httpPost()
    .jsonBody(json)
    .responseString()
  1. Handling responses with coroutines:
import com.github.kittinunf.fuel.coroutines.awaitStringResponseResult

suspend fun fetchData() {
    val (request, response, result) = "https://api.example.com/data"
        .httpGet()
        .awaitStringResponseResult()
    
    result.fold(
        { data -> println("Success: $data") },
        { error -> println("Error: ${error.message}") }
    )
}

Getting Started

To use Fuel in your Kotlin project, add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.github.kittinunf.fuel:fuel:2.3.1'
    implementation 'com.github.kittinunf.fuel:fuel-json:2.3.1' // For JSON support
}

Then, you can start making HTTP requests in your Kotlin code:

import com.github.kittinunf.fuel.httpGet

fun main() {
    val (_, _, result) = "https://api.example.com/data".httpGet().responseString()
    println(result)
}

Competitor Comparisons

42,958

A type-safe HTTP client for Android and the JVM

Pros of Retrofit

  • More mature and widely adopted in the Android ecosystem
  • Extensive documentation and community support
  • Annotation-based API definition for cleaner and more readable code

Cons of Retrofit

  • Steeper learning curve for beginners
  • Requires more setup and configuration
  • Limited to JSON parsing by default (requires additional libraries for other formats)

Code Comparison

Retrofit:

interface ApiService {
    @GET("users/{user}")
    fun getUser(@Path("user") user: String): Call<User>
}

val retrofit = Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .build()
val service = retrofit.create(ApiService::class.java)

Fuel:

Fuel.get("https://api.example.com/users/johndoe")
    .responseObject<User> { _, _, result ->
        when (result) {
            is Result.Success -> {
                val user = result.get()
            }
            is Result.Failure -> {
                val error = result.getException()
            }
        }
    }

Retrofit offers a more declarative approach with interface-based API definitions, while Fuel provides a simpler, more straightforward syntax for making HTTP requests. Retrofit requires more initial setup but offers cleaner code organization for complex APIs, whereas Fuel is easier to get started with and provides a more flexible approach to handling responses.

45,699

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

Pros of OkHttp

  • More mature and widely adopted library with extensive documentation
  • Supports HTTP/2 and web sockets out of the box
  • Offers advanced features like connection pooling and transparent GZIP compression

Cons of OkHttp

  • Steeper learning curve for beginners
  • Requires more boilerplate code for simple requests
  • Limited built-in support for coroutines (requires additional libraries)

Code Comparison

OkHttp:

val client = OkHttpClient()
val request = Request.Builder()
    .url("https://api.example.com/data")
    .build()
val response = client.newCall(request).execute()

Fuel:

val (_, _, result) = "https://api.example.com/data"
    .httpGet()
    .responseString()

Fuel offers a more concise syntax for simple requests, while OkHttp provides more granular control over the HTTP client and request configuration. OkHttp's approach may be preferred for complex networking scenarios, while Fuel's DSL-like syntax can be more appealing for quick and straightforward API calls.

Both libraries have their strengths, and the choice between them often depends on the specific requirements of the project and the developer's preferences.

An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries.

Pros of android-async-http

  • Mature and well-established library with a large user base
  • Extensive documentation and community support
  • Built-in support for common HTTP operations and authentication methods

Cons of android-async-http

  • Limited to Android platform
  • Uses callback-based approach, which can lead to callback hell in complex scenarios
  • Less modern syntax compared to Kotlin-based alternatives

Code Comparison

android-async-http:

AsyncHttpClient client = new AsyncHttpClient();
client.get("https://api.example.com", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        // Handle success
    }
});

Fuel:

Fuel.get("https://api.example.com")
    .responseString { result ->
        when (result) {
            is Result.Success -> // Handle success
            is Result.Failure -> // Handle failure
        }
    }

Summary

android-async-http is a mature and widely-used library for Android HTTP requests, offering extensive documentation and support. However, it's limited to Android and uses a callback-based approach. Fuel, on the other hand, is a modern Kotlin-based library that provides a more concise syntax and supports multiple platforms. Fuel's use of coroutines and Result types can lead to more readable and maintainable code, especially in complex scenarios.

🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀

Pros of Fast-Android-Networking

  • Built specifically for Android, offering optimized performance and Android-specific features
  • Supports both synchronous and asynchronous requests out of the box
  • Includes built-in caching mechanisms and image loading capabilities

Cons of Fast-Android-Networking

  • Limited to Android development, whereas Fuel is cross-platform (JVM, Android, iOS)
  • Less flexible in terms of customization compared to Fuel's modular architecture
  • Smaller community and potentially slower development cycle

Code Comparison

Fast-Android-Networking:

AndroidNetworking.get("https://api.example.com/data")
    .addQueryParameter("param", "value")
    .build()
    .getAsJSONObject(object : JSONObjectRequestListener {
        override fun onResponse(response: JSONObject) {
            // Handle response
        }
    })

Fuel:

"https://api.example.com/data"
    .httpGet(listOf("param" to "value"))
    .responseJson { _, _, result ->
        val (data, error) = result
        // Handle response
    }

Both libraries offer concise syntax for making HTTP requests, but Fast-Android-Networking provides a more Android-specific approach, while Fuel offers a more general, cross-platform solution. Fast-Android-Networking includes built-in JSON parsing, whereas Fuel requires additional setup for JSON handling.

3,376

Pros of Volley

  • Developed and maintained by Google, ensuring high reliability and support
  • Built-in request prioritization and cancellation capabilities
  • Efficient memory usage through automatic scheduling of network requests

Cons of Volley

  • Limited file upload and download functionality
  • Lack of built-in support for modern features like coroutines or Rx
  • More complex setup and configuration compared to Fuel

Code Comparison

Fuel:

val (request, response, result) = Fuel.get("https://api.example.com/data").responseString()

Volley:

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Handle response
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // Handle error
            }
        });

Fuel offers a more concise and Kotlin-friendly syntax, while Volley requires more boilerplate code but provides fine-grained control over request handling.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Fuel

Kotlin MavenCentral ktlint Run Gradle Push Codecov

The easiest HTTP networking library for Kotlin backed by Kotlinx Coroutines.

Migration

From 3.x onwards, we are using main as our new base branch. If you are finding the old version 2.x, please take a look at our old branch.

Download

For release version

implementation("com.github.kittinunf.fuel:fuel:3.0.0-alpha03")

Quick Start

use the any http method suspend function:

runBlocking {
    val string: String = Fuel.get("https://publicobject.com/helloworld.txt").body.string()
    println(string)
}

runBlocking {
    val string: String = "https://publicobject.com/helloworld.txt".httpGet().body.string()
    println(string)
}

runBlocking {
    val fuel = FuelBuilder().build()
    val string: String = fuel.get(request = { url = "https://publicobject.com/helloworld.txt" }).body.string()
    println(string)
}

Custom Configuration

JVM uses OkHttpClient configurations

val fuel = FuelBuilder().config(OKHttpClient()).build()
val string = fuel.get(request = { url = "https://publicobject.com/helloworld.txt" }).body.string()

Apple uses NSURLSessionConfiguration

val fuel = FuelBuilder().config(NSURLSessionConfiguration.defaultSessionConfiguration).build()
val string = fuel.get(request = { url = "https://publicobject.com/helloworld.txt" }).body.string()

Please note it will throw Exceptions. Make sure you catch it on the production apps.

Fuel requires Java 8 byte code.

Requirements

  • If you are using Android, It needs to be Android 5+.
  • Java 8+

R8 / Proguard

Fuel is fully compatible with R8 out of the box and doesn't require adding any extra rules.

If you use Proguard, you may need to add rules for Coroutines, OkHttp and Okio.

If you use the fuel-serialization modules, you may need to add rules for Serialization.

If you use the fuel-moshi modules, you may need to add rules for Moshi and Moshi-Kotlin

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 brought to you by contributors.

Licenses

Fuel released under the MIT license.