ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - elixir-tesla/tesla: The flexible HTTP client library for Elixir, with support for middleware and multiple adapters.
The flexible HTTP client library for Elixir, with support for middleware and multiple adapters. - elixir-tesla/tesla
Visit Site

GitHub - elixir-tesla/tesla: The flexible HTTP client library for Elixir, with support for middleware and multiple adapters.

GitHub - elixir-tesla/tesla: The flexible HTTP client library for Elixir, with support for middleware and multiple adapters.

Tesla

Test Hex.pm Hexdocs.pm Hex.pm Hex.pm codecov

Tesla is an HTTP client that leverages middleware to streamline HTTP requests and responses over a common interface for various adapters.

It simplifies HTTP communication by providing a flexible and composable middleware stack. Developers can easily build custom API clients by stacking middleware components that handle tasks like authentication, logging, and retries. Tesla supports multiple HTTP adapters such as Mint, Finch, Hackney, etc.

Tesla is ideal for developers who need a flexible and efficient HTTP client. Its ability to swap out HTTP adapters and create custom middleware pipelines empowers you to make different architectural decisions and build tools tailored to your application's needs with minimal effort.

Inspired by Faraday from Ruby.

Getting started

Add :tesla as dependency in mix.exs:

defp deps do
  [
     # or latest version
    {:tesla, "~> 1.11"},
    # optional, required by JSON middleware
    {:jason, "~> 1.4"},
    # optional, required by Mint adapter, recommended
    {:mint, "~> 1.0"}
  ]
end

:httpc as default Adapter {: .error}

The default adapter is erlang's built-in httpc, primarily to avoid additional dependencies when using Tesla in a new project. But it is not recommended to use it in production environment as it does not validate SSL certificates among other issues. Instead, consider using Mint, Finch, or Hackney adapters. We believe that such security issues should be addressed by :httpc itself and we are not planning to fix them in Tesla due to backward compatibility.

Configure default adapter in config/config.exs.

# config/config.exs

# Make sure to install `mint` package as well, recommended
config :tesla, adapter: Tesla.Adapter.Mint

To make a simple GET request, run iex -S mix and execute:

iex> Tesla.get!("https://httpbin.org/get").status
# => 200

That will not include any middleware and will use the global default adapter. Create a client to compose middleware and reuse it across requests.

iex> client = Tesla.client([
...>  {Tesla.Middleware.BaseUrl, "https://httpbin.org/"},
...>  Tesla.Middleware.JSON,
...> ])

iex> Tesla.get!(client, "/json").body
# => %{"slideshow" => ...}

Lastly, you can enforce the adapter to be used by a specific client:

iex> client = Tesla.client([], {Tesla.Adapter.Hackney, pool: :my_pool})

Happy hacking!

What to do next?

Check out the following sections to learn more about Tesla:

Explanations

Howtos

Migrations

References

Middleware

Tesla is built around the concept of composable middlewares.

  • Tesla.Middleware.BaseUrl - set base URL.
  • Tesla.Middleware.Headers - set request headers.
  • Tesla.Middleware.Query - set query parameters.
  • Tesla.Middleware.Opts - set request options.
  • Tesla.Middleware.FollowRedirects - follow HTTP 3xx redirects.
  • Tesla.Middleware.MethodOverride - set X-Http-Method-Override header.
  • Tesla.Middleware.Logger - log requests (method, url, status, and time).
  • Tesla.Middleware.KeepRequest - keep request body and headers.
  • Tesla.Middleware.PathParams - use templated URLs.
Formats
  • Tesla.Middleware.FormUrlencoded - URL encode POST body, useful for POSTing a map/keyword list.
  • Tesla.Middleware.JSON - encode/decode JSON request/response body.
  • Tesla.Middleware.Compression - compress request/response body using gzip and deflate.
  • Tesla.Middleware.DecodeRels - decode Link header into opts[:rels] field in response.
Auth
  • Tesla.Middleware.BasicAuth - HTTP Basic Auth.
  • Tesla.Middleware.BearerAuth - HTTP Bearer Auth.
  • Tesla.Middleware.DigestAuth] - Digest access authentication.
Error handling
  • Tesla.Middleware.Timeout - timeout request after X milliseconds despite of server response.
  • Tesla.Middleware.Retry - retry few times in case of connection refused.
  • Tesla.Middleware.Fuse - fuse circuit breaker integration.

Adapters

Tesla supports multiple HTTP adapter that do the actual HTTP request processing.

  • Tesla.Adapter.Httpc - the default, built-in Erlang httpc adapter.
  • Tesla.Adapter.Hackney - hackney, simple HTTP client in Erlang.
  • Tesla.Adapter.Ibrowse - ibrowse, Erlang HTTP client.
  • Tesla.Adapter.Gun - gun, HTTP/1.1, HTTP/2 and Websocket client for Erlang/OTP.
  • Tesla.Adapter.Mint - mint, Functional HTTP client for Elixir with support for HTTP/1 and HTTP/2.
  • Tesla.Adapter.Finch - finch, An HTTP client with a focus on performance, built on top of Mint and NimblePool.

Sponsors

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory