Req.Auth (req v0.8.0-rc.0)

Copy Markdown View Source

Sets request authentication.

For HTTP Digest authentication, when the response is HTTP 401 with a www-authenticate header, this step calculates the authorization: Digest ... header and makes another request.

Request Options

  • :auth - sets the authorization header:

    • string - sets to this value;

    • {:basic, userinfo} - uses Basic HTTP authentication;

    • {:digest, userinfo} - uses Digest HTTP authentication;

    • {:bearer, token} - uses Bearer HTTP authentication;

    • :netrc - load credentials from .netrc at path specified in NETRC environment variable. If NETRC is not set, load .netrc in user's home directory;

    • {:netrc, path} - load credentials from path

    • fn -> {:bearer, "eyJ0eXAi..." } end - a 0-arity function that returns one of the aforementioned types.

    • {mod, fun, args} - an MFArgs tuple that returns one of the aforementioned types.

Examples

iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: {:basic, "foo:foo"}).status
401
iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: {:basic, "foo:bar"}).status
200
iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: fn -> {:basic, "foo:bar"} end).status
200
iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: {Authentication, :fetch_token, []}).status
200

iex> Req.get!("https://httpbingo.org/digest-auth/auth/user/pass", auth: {:digest, "user:pass"}).status
200

iex> Req.get!("https://httpbingo.org/bearer", auth: {:bearer, ""}).status
401
iex> Req.get!("https://httpbingo.org/bearer", auth: {:bearer, "foo"}).status
200
iex> Req.get!("https://httpbingo.org/bearer", auth: fn -> {:bearer, "foo"} end).status
200

iex> System.put_env("NETRC", "./test/my_netrc")
iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: :netrc).status
200

iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: {:netrc, "./test/my_netrc"}).status
200
iex> Req.get!("https://httpbingo.org/basic-auth/foo/bar", auth: fn -> {:netrc, "./test/my_netrc"} end).status
200