mko*_*zun 2 erlang authorization http inets
如何在httpc:request()函数的http请求中为客户端授权指定用户/密码?
我不认为httpc
模块为此提供了便利.然而,实施并不困难(如果我们谈论基本认证).毕竟它只是一个额外的请求标题,其中'user:password'对Base64编码.例如,Tsung的ts_http_common模块可以做到这一点.
例如,以下是如何使用基本身份验证运行HTTP PUT请求:
auth_header(User, Pass) ->
Encoded = base64:encode_to_string(lists:append([User,":",Pass])),
{"Authorization","Basic " ++ Encoded}.
put_request(Url, User, Pass, Body) ->
ContentType = "text/json",
Headers = [auth_header(User, Pass), {"Content-Type",ContentType}],
Options = [{body_format,binary}],
httpc:request(put, {Url, Headers, ContentType, Body}, [], Options).
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2971 次 |
最近记录: |