您从文档中引用的关于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)
以下是您可以如何利用 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
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
,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/"
}}
格式化的输出是响应。在网站上webhook.site
,我可以看到请求(点击图片放大):
并设置授权密钥
Tesla 有两个用于授权的中间件模块:
Tesla.Middleware.BasicAuth
Tesla.Middleware.DigestAuth