Michael Lubas, 2022-08-17
In 1996 Google co-founder Larry Page posted in comp.lang.java, 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 Paraxial.io, 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.
mix.exs
defp deps do
[
{:finch, "~> 0.13.0"},
{:httpoison, "~> 1.8"},
{:req, "~> 0.3.0"},
{:tesla, "~> 1.4"},
{:hackney, "~> 1.18"}
]
end
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
# https://elixir-lang.org/getting-started/mix-otp/supervisor-and-application.html
Finch.start_link(name: MyFinch)
a = Finch.build(:get, "https://paraxial.io", [{"User-Agent", "UA-test-finch"}])
{:ok, %Finch.Response{body: body}} = Finch.request(a, MyFinch)
body
end
def get_httpoison do
{:ok, %HTTPoison.Response{body: body}} =
HTTPoison.get("https://paraxial.io", [{"User-Agent", "UA-test-httpoison"}])
body
end
def get_req do
# If you do Req.get!("https://paraxial.io", 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!("https://paraxial.io", user_agent: "UA-test-req")
body
end
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, "https://paraxial.io",
headers: [{"User-Agent", "UA-test-tesla-w-hackney"}]
)
body
end
Author contact: michael@paraxial.io
Paraxial.io is the only security platform built specifically for Elixir and Phoenix, helping developers and agents ship secure applications. Contact us today to get started.
Subscribe to stay up to date on new posts.