使用 Elixir HTTPoison 库创建 Github 令牌
Create Github Token using Elixir HTTPoison Library
我想使用 HTTPoison 库在 Elixir 中创建一个 Github 令牌,但我就是不知道如何发送 HTTPoison 参数。
使用curl
时,会变成this
$ curl -i -u "ColdFreak" -H "X-GitHub-OTP: 123456" -d '{"scopes": ["repo", "user"], "note"
: "getting-started"}' https://api.github.com/authorizations
当我使用 HTTPoison 库时,我就是不知道如何 post 它。
url = "https://api.github.com/authorizations"
HTTPoison.post!(url, [scopes: ["repo", "user"], note: "getting-started"], %{"X-GitHub-OTP" => "12345"})
然后它给出了类似这样的错误
** (ArgumentError) argument error
:erlang.iolist_to_binary([{"scopes", ["repo", "user"]}, {"note", "getting-started"}])
(hackney) src/hackney_client/hackney_request.erl:338: :hackney_request.handle_body/4
(hackney) src/hackney_client/hackney_request.erl:79: :hackney_request.perform/2
谁能告诉我正确的做法
HTTPPoison 的文档是 here
问题出在您的正文上 HTTPoison 需要格式为 {:form, [foo: "bar"]}
:
的二进制文件或元组
HTTPoison.post!(url, {:form, [scopes: "repo, user", note: "getting-started"]}, %{"X-GitHub-OTP" => "610554"})
或
HTTPoison.post!(url, "{\"scopes\": \"repo, user\", \"note\": \"getting-started\"}", %{"X-GitHub-OTP" => "610554"})
可以使用Poison库生成上面的JSON:
json = %{scopes: "repo, user", note: "getting-started"} |> Poison.encode!
HTTPoison.post!(url, json, %{"X-GitHub-OTP" => "610554"})
我想使用 HTTPoison 库在 Elixir 中创建一个 Github 令牌,但我就是不知道如何发送 HTTPoison 参数。
使用curl
时,会变成this
$ curl -i -u "ColdFreak" -H "X-GitHub-OTP: 123456" -d '{"scopes": ["repo", "user"], "note"
: "getting-started"}' https://api.github.com/authorizations
当我使用 HTTPoison 库时,我就是不知道如何 post 它。
url = "https://api.github.com/authorizations"
HTTPoison.post!(url, [scopes: ["repo", "user"], note: "getting-started"], %{"X-GitHub-OTP" => "12345"})
然后它给出了类似这样的错误
** (ArgumentError) argument error
:erlang.iolist_to_binary([{"scopes", ["repo", "user"]}, {"note", "getting-started"}])
(hackney) src/hackney_client/hackney_request.erl:338: :hackney_request.handle_body/4
(hackney) src/hackney_client/hackney_request.erl:79: :hackney_request.perform/2
谁能告诉我正确的做法
HTTPPoison 的文档是 here
问题出在您的正文上 HTTPoison 需要格式为 {:form, [foo: "bar"]}
:
HTTPoison.post!(url, {:form, [scopes: "repo, user", note: "getting-started"]}, %{"X-GitHub-OTP" => "610554"})
或
HTTPoison.post!(url, "{\"scopes\": \"repo, user\", \"note\": \"getting-started\"}", %{"X-GitHub-OTP" => "610554"})
可以使用Poison库生成上面的JSON:
json = %{scopes: "repo, user", note: "getting-started"} |> Poison.encode!
HTTPoison.post!(url, json, %{"X-GitHub-OTP" => "610554"})