Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inets http client + authorization

How should I specify user/password for client authorization in http request made by httpc:request() function??

like image 798
mkorszun Avatar asked Jun 16 '26 14:06

mkorszun


2 Answers

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). 
like image 118
Ed'ka Avatar answered Jun 18 '26 12:06

Ed'ka


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

like image 35
user425720 Avatar answered Jun 18 '26 14:06

user425720