chensan

chensan

I’m in China, we have this so-called great firewall blocking access to some sites.

So we employ some proxies to bypass it, like running

export http_proxy=ip:port;export https_proxy=ip:port

command under my terminal.

After doing that, I would expect HTTPoison to use the proxy ip:port, but obviously it’s not in my test, so I have to pass the proxy option myself:

HTTPoison.get! "https://sites-get-blocked.org", [], [{:proxy, "http://127.0.0.1:8888"}]

My question is, does it respect the system proxy setting? Anyone has a better way to handle a problem like this?

Showing Posts 1 to 8

NobbZ

NobbZ

Nope, neither HTTPoison nor HTTpotion, nor their underlying erlang libraries do that as far as a quick google turns out. But it should be easy to write a wrapper which looks up the environment variables and then appends the proxy option if it is not already there.

chensan

chensan OP

Yeah, this is what I do at last:

defmodule TelegramBot do
  use HTTPoison.Base

  @endpoint "https://api.telegram.org/bot"
  @token Application.get_env(:myflow, Telegram) |> Keyword.fetch!(:token)
  @proxy Application.get_env(:myflow, Telegram) |> Keyword.get(:proxy)

  def process_url(url) do
    @endpoint <> @token <> url
  end

  defp process_request_options(options) do
    if @proxy do Keyword.merge(options, [proxy: @proxy]) else options end
  end
end

When there’s proxy presented in dev.exs, pass it to HTTPoison.

NobbZ

NobbZ

Why don’t you use System.get_env/1?

chensan

chensan OP

Wow, I think it’s much better with System.get_env, thanks!

sriehl

sriehl

Be careful with using System.get_env/1. It will use the environment variables that are set when your app is compiled. It does not use the environment variables at runtime.

For more info: How to config environment variables with Elixir and Exrm « Plataformatec Blog

gregvaughn

gregvaughn

It’s not quite that simple. Well, now that I think about it, it might actually be even simpler than that. System.get_env/1 gets the environment variable at the moment it is executed. Full stop. There’s nothing special about this function doing different things at compile time or run time.

The challenge is understanding when it is executed. If used in a module attribute or macro, it gets executed at compile time. If used in a plain function, it gets executed at run time. If put in a config/*.exs file, it gets executed when mix executes. And this paragraph is true of any function. It’s nothing specific to System.get_env/1

NobbZ

NobbZ

We are not talking about using it in mix.exs or config/*.exs, we are talking about having it straight in the application. In a wrapper around another function call.

jackalcooper

jackalcooper

I think that was because you call it in Elixir’s config file which is consolidated at compile time.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews