Edited at

HTTPoisonでx-www-form-urlencodedでPOST

More than 1 year has passed since last update.

post!の第二引数を{:form, keywordlist}にする。

ドキュメントにもちゃんとかいてあって Body:のところにかいてある。


{:form, [{K, V}, ...]} - send a form url encoded

https://hexdocs.pm/httpoison/HTTPoison.html#request/5



sample.exs

url = "http://httpbin.org/post"

params = [hoge: 1]
%HTTPoison.Response{body: body} = HTTPoison.post!(url, {:form, params})
IO.puts(body)

$ mix sample.exs | jq .

{
"args": {},
"data": "",
"files": {},
"form": {
"hoge": "1"
},
"headers": {
"Connection": "close",
"Content-Length": "6",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Host": "httpbin.org",
"User-Agent": "hackney/1.10.1"
},
"json": null,
"origin": "180.220.192.160",
"url": "http://httpbin.org/post"
}

$ mix sample.exs | jq .form

{
"hoge": "1"
}


関連


mix.exs


mix.exs

defmodule HttpoisonSample.Mixfile do

use Mix.Project

def project do
[
app: :httpoison_sample,
version: "0.1.0",
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:httpoison, "~> 0.13"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
]
end
end