6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HTTPoisonでx-www-form-urlencodedでPOST

Last updated at Posted at 2018-01-04

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
6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?