changed README.md
 
@@ -69,6 69,8 @@ case HTTPoison.get(url) do
69
69
end
70
70
```
71
71
72
[Here](https://www.erlang.org/doc/man/inet.html#posix-error-codes) is the list of all possible error reasons.
73
72
74
### Options
73
75
74
76
There are a number of supported options(*not to be confused with the HTTP options method*), documented [here](https://hexdocs.pm/httpoison/HTTPoison.html#request/5), that can be added to your request. The example below shows the use of the `:ssl` and `:recv_timeout` options for a post request to an api that requires a bearer token. The `:ssl` option allows you to set options accepted by the [Erlang SSL module](https://erlang.org/doc/man/ssl.html), and `:recv_timeout` sets a timeout on receiving a response, the default is 5000ms.
changed hex_metadata.config
 
@@ -20,4 20,4 @@
20
20
{<<"optional">>,false},
21
21
{<<"repository">>,<<"hexpm">>},
22
22
{<<"requirement">>,<<"~> 1.17">>}]]}.
23
- {<<"version">>,<<"1.8.1">>}.
23
{<<"version">>,<<"1.8.2">>}.
changed lib/httpoison.ex
 
@@ -55,6 55,108 @@ defmodule HTTPoison.Request do
55
55
params: params,
56
56
options: options
57
57
}
58
59
@doc """
60
Returns an equivalent `curl` command for the given request.
61
62
## Examples
63
iex> request = %HTTPoison.Request{url: "https://api.github.com", method: :get, headers: [{"Content-Type", "application/json"}]}
64
iex> HTTPoison.Request.to_curl(request)
65
"curl -X GET -H 'Content-Type: application/json' https://api.github.com ;"
66
67
iex> request = HTTPoison.get!("https://api.github.com", [{"Content-Type", "application/json"}]).request
68
iex> HTTPoison.Request.to_curl(request)
69
"curl -X GET -H 'Content-Type: application/json' https://api.github.com ;"
70
"""
71
@spec to_curl(t()) :: {:ok, binary()} | {:error, atom()}
72
def to_curl(request = %__MODULE__{}) do
73
options =
74
Enum.reduce(request.options, [], fn
75
{:timeout, timeout}, acc ->
76
["--connect-timeout #{Float.round(timeout / 1000, 3)}" | acc]
77
78
{:recv_timeout, timeout}, acc ->
79
["--max-time #{Float.round(timeout / 1000, 3)}" | acc]
80
81
{:proxy, {:socks5, host, port}}, acc ->
82
proxy_auth =
83
if request.options[:socks5_user] do
84
user = request.options[:socks5_user]
85
pass = request.options[:socks5_pass]
86
" --proxy-basic --proxy-user #{user}:#{pass}"
87
end
88
89
["--socks5 #{host}:#{port}#{proxy_auth}" | acc]
90
91
{:proxy, {host, port}}, acc ->
92
["--proxy #{host}:#{port}" | acc]
93
94
{:proxy_auth, {user, pass}}, acc ->
95
["--proxy-user #{user}:#{pass}" | acc]
96
97
{:ssl, ssl_opts}, acc ->
98
ssl_opts =
99
Enum.reduce(ssl_opts, [], fn
100
{:keyfile, keyfile}, acc -> ["--key #{keyfile}" | acc]
101
{:certfile, certfile}, acc -> ["--cert #{certfile}" | acc]
102
{:cacertfile, cacertfile}, acc -> ["--cacert #{cacertfile}" | acc]
103
end)
104
|> Enum.join(" ")
105
106
[ssl_opts | acc]
107
108
{:follow_redirect, true}, acc ->
109
max_redirs = Keyword.get(request.options, :max_redirect, 5)
110
["-L --max-redirs #{max_redirs}" | acc]
111
112
{:hackney, _}, _ ->
113
throw({:error, :hackney_opts_not_supported})
114
115
_, acc ->
116
acc
117
end)
118
|> Enum.join(" ")
119
120
{scheme_opts, url} =
121
case URI.parse(request.url) do
122
%URI{scheme: "http unix"} = uri ->
123
uri = %URI{uri | scheme: "http", host: nil, authority: nil}
124
{"--unix-socket #{uri.host}", URI.to_string(uri)}
125
126
_ ->
127
{"", request.url}
128
end
129
130
method = "-X " <> (request.method |> to_string() |> String.upcase())
131
headers = request.headers |> Enum.map(fn {k, v} -> "-H '#{k}: #{v}'" end) |> Enum.join(" ")
132
133
body =
134
case request.body do
135
"" -> ""
136
{:file, filename} -> "-d @#{filename}"
137
{:form, form} -> form |> Enum.map(fn {k, v} -> "-F '#{k}=#{v}'" end) |> Enum.join(" ")
138
{:stream, stream} -> "-d '#{Enum.join(stream, "")}'"
139
{:multipart, _} -> throw({:error, :multipart_not_supported})
140
body when is_binary(body) -> "-d '#{body}'"
141
_ -> ""
142
end
143
144
{:ok,
145
[
146
"curl",
147
options,
148
scheme_opts,
149
method,
150
headers,
151
body,
152
url
153
]
154
|> Enum.map(&String.trim/1)
155
|> Enum.filter(&(&1 != ""))
156
|> Enum.join(" ")}
157
catch
158
e -> e
159
end
58
160
end
59
161
60
162
defmodule HTTPoison.Response do
changed lib/httpoison/base.ex
 
@@ -90,7 90,7 @@ defmodule HTTPoison.Base do
90
90
@callback delete!(url, headers, options) :: Response.t() | AsyncResponse.t() | MaybeRedirect.t()
91
91
92
92
@callback get(url) :: {:ok, Response.t() | AsyncResponse.t()} | {:error, Error.t()}
93
- @callback get(url, headers) :: {:ok, Response.t() | AsyncResponse.t() | {:error, Error.t()}}
93
@callback get(url, headers) :: {:ok, Response.t() | AsyncResponse.t()} | {:error, Error.t()}
94
94
@callback get(url, headers, options) ::
95
95
{:ok, Response.t() | AsyncResponse.t()} | {:error, Error.t()}
changed mix.exs
 
@@ -7,7 7,7 @@ defmodule HTTPoison.Mixfile do
7
7
def project do
8
8
[
9
9
app: :httpoison,
10
- version: "1.8.1",
10
version: "1.8.2",
11
11
elixir: "~> 1.9",
12
12
name: "HTTPoison",
13
13
description: @description,