我是erlang生态系统的新手,我正在寻找相当于这个curl命令的
curl 'http://localhost/api/foo/' \
-H 'Authorization: Bearer abc123'我希望尽可能地保持“核心库”的专注(例如,在REPL上的ruby工作中的这个例子)。
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发布于 2014-09-09 16:22:53
使用内情。这是标准Erlang发行版的一部分,它允许您发送带有自定义标头的get和post请求。
如何使用inets作为http客户端
要执行的代码,您需要的是:
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]).https://stackoverflow.com/questions/25748917
复制相似问题