Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TrevorS/eedbb1e109b6c02d78ce to your computer and use it in GitHub Desktop.
Save TrevorS/eedbb1e109b6c02d78ce to your computer and use it in GitHub Desktop.
defmodule QueryUtil do
def add_params_to_url(url, params) do
url <> query_string(params)
end
defp query_string(params) do
params |> Enum.map(&("#{&1.name}=#{&1.value}")) |> Enum.join("&")
end
end
url = "http://example.com?"
params = [
%{name: "test1", value: "value1"},
%{name: "test2", value: "value2"}
]
result = QueryUtil.add_params_to_url(url, params)
IO.puts "result: #{inspect result}"
# ➜ ~ elixir query_util.exs
# result: "http://example.com?test1=value1&test2=value2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment