Ran*_*mir 1 elixir tesla phoenix-framework httpoison
我正在尝试使用 tesla 发出帖子请求,但出现错误:
(CaseClauseError) no case clause matching: %{name: "ecdebit"}
(hackney) /deps/hackney/src/hackney_request.erl:312::hackney_request.handle_body/4
(hackney) /deps/hackney/src/hackney_request.erl:81::hackney_request.perform/2
(hackney) /deps/hackney/src/hackney.erl:372::hackney.send_request/2
(tesla) lib/tesla/adapter/hackney.ex:69: Tesla.Adapter.Hackney.request/5
(tesla) lib/tesla/adapter/hackney.ex:31: Tesla.Adapter.Hackney.call/2
Run Code Online (Sandbox Code Playgroud)
我的请求代码是
request_body = %{
name: "ecdebit",
}
Tesla.post(client, "/contactdb/lists", request_body)
Run Code Online (Sandbox Code Playgroud)
在特斯拉基本网址是:https://api.sendgrid.com/v3并设置授权key。我们如何为 post 请求传递数据?
在特斯拉文档中,将发布请求定义为:
Tesla.post("http://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
Run Code Online (Sandbox Code Playgroud)
这个星球上有没有人可以帮助摆脱这个小故障:(。
您从文档中引用的关于Tesla.post():
Tesla.post("http://httpbin.org/post",
"data",
headers: [{"content-type", "application/json"}])
Run Code Online (Sandbox Code Playgroud)
显示第一个参数是一个包含 url 的字符串,第二个参数是一个包含数据的字符串,第三个参数是一个关键字列表。然而,当你调用时,Tesla.post()你传递一个路径作为第二个参数,一个 Elixir 映射作为第三个参数:
Tesla.post(client,
"/contactdb/lists",
request_body)
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式利用 Tesla 的功能逐步将您的请求转换为您想要的内容:
1)创建一个混合项目:
~/elixir_programs$ mix new http
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/http.ex
* creating test
* creating test/test_helper.exs
* creating test/http_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd http
mix test
Run "mix help" for more commands.
~/elixir_programs$ cd http
Run Code Online (Sandbox Code Playgroud)
2)在mix.ex文件中添加jason、hackney、tesla作为依赖:
defp deps do
[
{:jason, "~> 1.0"}, #for tesla JSON middleware
{:hackney, "~> 1.14.0"}, #recommended for tesla
{:tesla, "~> 1.2.1"}, #http requests
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
]
end
Run Code Online (Sandbox Code Playgroud)
3) 更改lib/http.ex为以下内容:
defmodule Http do
use Tesla
plug Tesla.Middleware.BaseUrl, "https://webhook.site/ab8b7259-feb4-4e62-b8dd-46bb03b614ba"
plug Tesla.Middleware.FormUrlencoded
#plug Tesla.Middleware.Headers, [{"header-name", "header-value"}]
def go do #Use this function to test whether a Tesla request succeeds without having to repeatedly type the following variables and their values into iex.
request_body = %{name: "Joel", age: 21}
path = "/"
post(path, request_body) #Outside of this module, you would need to write Http.post(...)
end
end
Run Code Online (Sandbox Code Playgroud)
当您指定 时Tesla.Middleware.BaseUrl,Tesla 会自动将基本 url 附加到您指定为post()函数中第一个参数的字符串(除非字符串以“http”或“https”开头)。
当您指定 时Tesla.Middleware.FormUrlencoded,Tesla 会自动将作为post()函数的第二个参数给出的 Elixir 映射转换为网页上的表单发送到服务器的格式,如下所示:
"name=Joel&age=21"
Run Code Online (Sandbox Code Playgroud)
Tesla 还会自动将以下标头添加到请求中:
Contet-Type: application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
如果您希望 Tesla 将 Elixir 映射转换为 json 字符串,请Tesla.Middleware.JSON改为指定(注意这需要jason在您的依赖项中)。Tesla 还会自动将以下标头添加到请求中:
Contet-Type: application/json
Run Code Online (Sandbox Code Playgroud)
当您指定 时Tesla.Middleware.Headers,Tesla 会自动将指定的标头添加到您的所有请求中。
现在在 iex 中尝试一下:
~/elixir_programs/http$ iex -S Mix
...
...
iex(1)> {:ok, response} = Http.go
{:ok,
%Tesla.Env{
__client__: %Tesla.Client{adapter: nil, fun: nil, post: [], pre: []},
__module__: Http,
body: "",
headers: [
{"cache-control", "no-cache"},
{"connection", "keep-alive"},
{"date", "Thu, 29 Nov 2018 03:56:49 GMT"},
{"server", "nginx/1.10.3"},
{"vary", "Accept-Encoding"},
{"content-length", "0"},
{"content-type", "text/plain; charset=UTF-8"},
{"x-request-id", "55a92302-6459-49bc-b7eb-f3e7ff037599"},
{"x-token-id", "ab8b7259-feb4-4e62-b8dd-46bb03b614ba"},
{"x-ratelimit-limit", "30"},
{"x-ratelimit-remaining", "29"},
{"strict-transport-security", "max-age=63072000; includeSubdomains"},
{"x-frame-options", "DENY"},
{"x-content-type-options", "nosniff"}
],
method: :post,
opts: [],
query: [],
status: 200,
url: "https://webhook.site/ab8b7259-feb4-4e62-b8dd-46bb03b614ba/"
}}
Run Code Online (Sandbox Code Playgroud)
格式化的输出是响应。在网站上webhook.site,我可以看到请求(点击图片放大):
并设置授权密钥
Tesla 有两个用于授权的中间件模块:
Tesla.Middleware.BasicAuth
Tesla.Middleware.DigestAuth
Run Code Online (Sandbox Code Playgroud)