How should I specify user/password for client authorization in http request made by httpc:request() function??
I don't think httpc module provides facility for that.
Nevertheless it isn't hard to implement (if we are talking about Basic Authentification). After all it's just an additional request header with 'user:password' pair Base64 encoded.
For example Tsung's ts_http_common module does it.
For instance, here is how you can run HTTP PUT request with basic authentication:
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).
I see in doc that HTTPOptions holds pass and user:
HTTPOptions = http_options()
http_options() = [http_option()]
http_option() = {timeout, timeout()} | {connect_timeout, timeout()} | {ssl, ssloptions()} | {ossl, ssloptions()} | {essl, ssloptions()} | {autoredirect, boolean()} | {proxy_auth, {userstring(), passwordstring()}} | {version, http_version()} | {relaxed, boolean()} | {url_encode, boolean()}
documentation: http://www.erlang.org/doc/man/httpc.html#request-5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With