我正在尝试使用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
我的请求代码是
request_body = %{
name: "ecdebit",
}
Tesla.post(client, "/contactdb/lists", request_body)
特斯拉基本URL中的是:https://api.sendgrid.com/v3
,还设置了授权key
。我们如何传递数据以进行后期请求?
与tesla文档中的一样,将发布请求定义为:
Tesla.post("http://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
在这个星球上,有没有人可以帮助摆脱这种故障:(。
答案 0 :(得分:3)
您对Tesla.post()
文档的引用:
Tesla.post("http://httpbin.org/post",
"data",
headers: [{"content-type", "application/json"}])
显示第一个参数是包含url的字符串,第二个参数是包含数据的字符串,第三个参数是关键字列表。但是,当您调用Tesla.post()
时,您将路径作为第二个参数,并将Elixir映射作为第三个参数:
Tesla.post(client,
"/contactdb/lists",
request_body)
在这里,您可以利用特斯拉的功能将请求逐步转换为所需的内容:
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
2)将jason,hackney和tesla作为依赖项添加到mix.ex
文件中:
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
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
当您指定Tesla.Middleware.BaseUrl
时,Tesla将自动将基本URL放在您指定为post()
函数中第一个参数的字符串之前(除非该字符串以“ http”或“ https”开头) 。
当您指定Tesla.Middleware.FormUrlencoded
时,Tesla会自动将作为post()
函数的第二个参数给出的Elixir映射转换为网页上的表单发送到服务器的格式,看起来像像这样:
"name=Joel&age=21"
Tesla还会自动将以下标头添加到请求中:
Contet-Type: application/x-www-form-urlencoded
如果您希望Tesla将Elixir映射转换为json字符串,请改为指定Tesla.Middleware.JSON
(请注意,这需要jason
在依赖项中)。 Tesla还将自动将以下标头添加到请求中:
Contet-Type: application/json
当您指定Tesla.Middleware.Headers
时,特斯拉会自动将指定的标头添加到您的所有请求中。
现在在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/"
}}
格式化的输出是响应。在网站webhook.site
上,我可以看到请求(单击图像将其放大):
并设置授权密钥
特斯拉有two middleware modules for authorization:
Tesla.Middleware.BasicAuth
Tesla.Middleware.DigestAuth