You can use Fixie with any Elixir HTTP client. Below is an example of using it with the popular Req
library.
Using Req
First, you need to add the Req
library to your project if you haven't already:
defp deps do
[
{:req, "~> 0.3.0"}
]
end
Next, configure the HTTP request to use a proxy. The following example demonstrates how to set up proxy options by extracting the necessary environment variables and adding basic authentication to the proxy headers:
...
import Base
import URI
def connect_options() do
fixie_url = System.get_env("FIXIE_URL")
%URI{userinfo: userinfo, host: proxy_host} = URI.parse(fixie_url)
[proxy_username, proxy_password] = String.split(userinfo, ":")
proxy_auth = Base.encode64("#{proxy_username}:#{proxy_password}")
[
proxy: {:http, proxy_host, 80, []},
proxy_headers: [
{"Proxy-Authorization", "Basic #{proxy_auth}"}
]
]
end
def post_request(url, form) do
Req.post!(url, form: form, connect_options: connect_options())
end
In the example above:
proxy_username
,proxy_password
, andproxy_host
are parsed fromFIXIE_URL
variables.proxy_auth
is encoded usingBase.encode64
to create the authorization header.- The
connect_options
function returns a list of options, including the proxy settings and headers, which are then used in theReq.post!/3
call.
To make a POST request through the proxy using Req
, you can call the post_request/2
function:
post_request("http://welcome.usefixie.com", %{key: "value"})
This configuration will route the HTTP request through the specified proxy, including the necessary authentication.
Having issues? Please reach out to our team here.