我是 erlang 生态系统的新手,我正在寻找与此 curl 命令等效的命令
curl 'http://localhost/api/foo/' \
-H 'Authorization: Bearer abc123'
Run Code Online (Sandbox Code Playgroud)
我想尽可能地专注于“核心库”(即 - 这个 ruby 示例在 REPL 上开箱即用)
require 'net/http'
uri = URI('http://localhost/api/foo/')
req = Net::HTTP::Get.new(uri)
req['Authorization'] = 'Bearer abc123'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts res.body
Run Code Online (Sandbox Code Playgroud)
使用inets。这是标准 Erlang 发行版的一部分,它允许您使用自定义标头发送 get 和 post 请求。
以及要执行的代码,您需要什么:
inets:start(),
Url = "http://localhost/api/foo/",
AuthHeader = {"Authorization", "Bearer abc123"},
{ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
httpc:request(get,
{Url, [AuthHeader]},
[],
[]),
io:format("~s", [Body]).
Run Code Online (Sandbox Code Playgroud)