如何发出httpc请求并附加客户端证书?

How to make httpc request and attach client certificate?

我有一个 IIS 托管站点,它要求将客户端证书附加到任何传入请求。我正在尝试从 Erlang 发出请求,但无法弄清楚如何将客户端附加到传出的 httpc 请求。我的示例代码:

inets:start(),
ssl:start(),
httpc:request(get, {"https://my-iis-host.org/api/endpoint?parm1=foobar", []}, [{ssl, [{cacertfile, "/path/to/my/cert.pem"}]}], []) . 

returns 来自服务器的 403.7 响应。我确定证书很好,因为我可以使用 curl 并成功进行相同的调用并获得成功的响应:

curl -v --cert /path/to/my/cert.pem "https://my-iis-host.org/api/endpoint?parm1=foobar"

我假设我的 erlang 调用是错误的,它实际上没有将客户端证书附加到请求,但它应该如何工作?感谢您的帮助!

编辑:最终我试图解决的问题实际上是 POST 数据到端点并附加客户端证书。为简单起见,我用 GET 复制了它,但想分享我的完整 POST 示例,该示例最终有效,以防其他人 运行 遇到相同的问题:

inets:start(),
ssl:start(),
Body = "{'name':'value','name1':'value1'}",
Request = {"https://my-iis-host.org/api/endpoint", [], "application/json", Body},
httpc:request(post, Request, [{ssl, [{certfile, "/path/to/my/cert.pem"}]}], []) . 

我认为您需要使用 certfile 选项而不是 cacertfile。尝试

httpc:request(get, {"https://my-iis-host.org/api/endpoint?parm1=foobar", []}, [{ssl, [{certfile, "/path/to/my/cert.pem"}]}], []).` 

curl 有这些选项可用:

--cert
--cacert

您选择使用 --cert 选项,您的 curl 请求成功。

erlang 为 ssloption() 列出了以下选项:

ssloption() = {verify, verify_type()} | {verify_fun, {fun(), term()}} | {fail_if_no_peer_cert, boolean()} {depth, integer()} | {cert, der_encoded()}| {certfile, path()} | {key, {'RSAPrivateKey'| 'DSAPrivateKey' | 'ECPrivateKey' |'PrivateKeyInfo', der_encoded()}} | {keyfile, path()} | {password, string()} | {cacerts, [der_encoded()]} | {cacertfile, path()} | |{dh, der_encoded()} | {dhfile, path()} | {ciphers, ciphers()} | {user_lookup_fun, {fun(), term()}}, {psk_identity, string()}, {srp_identity, {string(), string()}} | {ssl_imp, ssl_imp()} | {reuse_sessions, boolean()} | {reuse_session, fun()} {next_protocols_advertised, [binary()]} | {client_preferred_next_protocols, {client | server, [binary()]} | {client | server, [binary()], binary()}} | {log_alert, boolean()} | {server_name_indication, hostname() | disable}

为了与您的 curl 请求保持一致,人们会认为您会选择 {certfile, path()}