Setting User-Agent in Elixir with Finch, HTTPoison, Req, or Tesla

Michael Lubas, 2022-08-17 Printing User Agents

In 1996 Google co-founder Larry Page posted in, Q: Setting User-Agent Field?. 26 years later, you may still need to set the User-Agent in your project. Here are four examples from the Elixir HTTP clients Finch, HTTPoison, Req, and Tesla.

The above screenshot is from, which shows the User-Agent of incoming HTTP requests to a Phoenix application. If you don’t have an account use ngrok to verify what User-Agent you’re sending.


  defp deps do
      {:finch, "~> 0.13.0"},
      {:httpoison, "~> 1.8"},
      {:req, "~> 0.3.0"},
      {:tesla, "~> 1.4"},
      {:hackney, "~> 1.18"}
defmodule Multitude do
  def get_finch do
    # Typically you just add Finch to your app's supervision tree
    # If you don't know what that means, read
    Finch.start_link(name: MyFinch)
    a =, "", [{"User-Agent", "UA-test-finch"}])
    {:ok, %Finch.Response{body: body}} = Finch.request(a, MyFinch)

  def get_httpoison do
    {:ok, %HTTPoison.Response{body: body}} =
      HTTPoison.get("", [{"User-Agent", "UA-test-httpoison"}])

  def get_req do
    # If you do Req.get!("", headers: [{"User-Agent", "UA-test-req"}])
    # The user agent gets set to "UA-test-req, req/0.3.0"
    # So make sure you use the user_agent: option below
    %Req.Response{body: body} =
      Req.get!("", user_agent: "UA-test-req")

  def get_tesla do
    # With Hackney adapter for correct SSL/TLS settings
    middleware = []
    adapter = {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}
    client = Tesla.client(middleware, adapter)
    {:ok, %Tesla.Env{body: body}} =
      Tesla.get(client, "",
        headers: [{"User-Agent", "UA-test-tesla-w-hackney"}]

Author contact: stops data breaches by securing your Elixir and Phoenix apps. Detect and fix critical security issues today.

Subscribe to stay up to date on new posts.