View Source Req (req v0.3.0-dev)
The high-level API.
Req is composed of three main pieces:
Req- the high-level API (you're here!)Req.Request- the low-level API and the request structReq.Steps- the collection of built-in steps
The high-level API is what most users of Req will use most of the time.
examples
Examples
Making a GET request with Req.get!/1:
iex> Req.get!("https://api.github.com/repos/elixir-lang/elixir").body["description"]
"Elixir is a dynamic, functional language designed for building scalable and maintainable applications"Same, but by explicitly building request struct first:
iex> req = Req.new(base_url: "https://api.github.com")
iex> Req.get!(req, url: "/repos/elixir-lang/elixir").body["description"]
"Elixir is a dynamic, functional language designed for building scalable and maintainable applications"Making a POST request with Req.post!/2:
iex> Req.post!("https://httpbin.org/post", form: [comments: "hello!"]).body["form"]
%{"comments" => "hello!"}
Link to this section Summary
Functions
Returns default options.
Sets default options for Req.new/1.
Makes a DELETE request.
Makes a GET request.
Makes a HEAD request.
Returns a new request struct with built-in steps.
Makes a PATCH request.
Makes a POST request.
Makes a PUT request.
Makes an HTTP request and returns a response or raises an error.
Makes an HTTP request and returns a response or raises an error.
Makes an HTTP request.
Makes an HTTP request.
Link to this section Types
Link to this section Functions
@spec default_options() :: keyword()
Returns default options.
See default_options/1 for more information.
@spec default_options(keyword()) :: :ok
Sets default options for Req.new/1.
Avoid setting default options in libraries as they are global.
examples
Examples
iex> Req.default_options(base_url: "https://httpbin.org")
iex> Req.get!("/statuses/201").status
201
iex> Req.new() |> Req.get!(url: "/statuses/201").status
201
@spec delete!(url() | Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes a DELETE request.
See request/1 for a list of supported options.
examples
Examples
With URL:
iex> Req.delete!("https://httpbin.org/anything").body["method"]
"DELETE"With request struct:
iex> req = Req.new(url: "https://httpbin.org/anything")
iex> Req.delete!(req).body["method"]
"DELETE"
@spec get!(url() | Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes a GET request.
See request/1 for a list of supported options.
examples
Examples
With URL:
iex> Req.get!("https://api.github.com/repos/elixir-lang/elixir").body["description"]
"Elixir is a dynamic, functional language designed for building scalable and maintainable applications"With request struct:
iex> req = Req.new(base_url: "https://api.github.com")
iex> Req.get!(req, url: "/repos/elixir-lang/elixir").status
200
@spec head!(url() | Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes a HEAD request.
See request/1 for a list of supported options.
examples
Examples
With URL:
iex> Req.head!("https://httpbin.org/status/201").status
201With request struct:
iex> req = Req.new(base_url: "https://httpbin.org")
iex> Req.head!(req, url: "/status/201").status
201
@spec new(options :: keyword()) :: Req.Request.t()
Returns a new request struct with built-in steps.
See request/1 for a list of available options. See Req.Request module documentation
for more information on the underlying request struct.
examples
Examples
iex> req = Req.new(url: "https://elixir-lang.org")
iex> req.method
:get
iex> URI.to_string(req.url)
"https://elixir-lang.org"
@spec patch!(url() | Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes a PATCH request.
See request/1 for a list of supported options.
examples
Examples
With URL:
iex> Req.patch!("https://httpbin.org/anything", body: "hello!").body["data"]
"hello!"With request struct:
iex> req = Req.new(url: "https://httpbin.org/anything")
iex> Req.patch!(req, body: "hello!").body["data"]
"hello!"
@spec post!(url() | Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes a POST request.
See request/1 for a list of supported options.
examples
Examples
With URL:
iex> Req.post!("https://httpbin.org/anything", body: "hello!").body["data"]
"hello!"
iex> Req.post!("https://httpbin.org/anything", form: [x: 1]).body["form"]
%{"x" => "1"}
iex> Req.post!("https://httpbin.org/anything", json: %{x: 2}).body["json"]
%{"x" => 2}With request struct:
iex> req = Req.new(url: "https://httpbin.org/anything")
iex> Req.post!(req, body: "hello!").body["data"]
"hello!"
@spec put!(url() | Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes a PUT request.
See request/1 for a list of supported options.
examples
Examples
With URL:
iex> Req.put!("https://httpbin.org/anything", body: "hello!").body["data"]
"hello!"With request struct:
iex> req = Req.new(url: "https://httpbin.org/anything")
iex> Req.put!(req, body: "hello!").body["data"]
"hello!"
@spec request!(Req.Request.t() | keyword()) :: Req.Response.t()
Makes an HTTP request and returns a response or raises an error.
See request/1 for more information.
examples
Examples
iex> Req.request!(url: "https://api.github.com/repos/elixir-lang/elixir").status
200
@spec request!(Req.Request.t(), options :: keyword()) :: Req.Response.t()
Makes an HTTP request and returns a response or raises an error.
See request/1 for more information.
examples
Examples
iex> req = Req.new(base_url: "https://api.github.com")
iex> Req.request!(req, url: "/repos/elixir-lang/elixir").status
200
@spec request(Req.Request.t() | keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()}
Makes an HTTP request.
request/1 and request/2 functions give three ways of making requests:
With a list of options, for example:
Req.request(url: url)With a request struct, for example:
Req.new(url: url) |> Req.request()With a request struct and more options, for example:
Req.new(base_url: base_url) |> Req.request(url: url)
This function as well as all the other ones in this module accept the same set of options described below.
options
Options
Basic request options:
:method- the request method, defaults to:get.:url- the request URL.:headers- the request headers.The headers are automatically encoded using these rules:
atom header names are turned into strings, replacing
-with_. For example,:user_agentbecomes"user-agent"string header names are left as is. Because header keys are case-insensitive in both HTTP/1.1 and HTTP/2, it is recommended for header keys to be in lowercase, to avoid sending duplicate keys in a request.
NaiveDateTimeandDateTimeheader values are encoded as "HTTP date". Otherwise, the header value is encoded withString.Chars.to_string/1.
If you set
:headersoptions both inReq.new/1andrequest/2, the header lists are merged.:body- the request body.
Additional URL options:
:base_url- if set, the request URL is prepended with this base URL (viaput_base_urlstep).:params- if set, appends parameters to the request query string (viaput_paramsstep).
Authentication options:
:auth- sets request authentication (viaauthstep).
Request body options:
:form- if set, encodes the request body as form data (encode_bodystep).:json- if set, encodes the request body as JSON (encode_bodystep).:compress_body- if set totrue, compresses the request body using gzip (viacompress_bodystep). Defaults tofalse.
Response body options:
:compressed- if set totrue, asks the server to return compressed response. (viacompressedstep). Defaults totrue.:raw- if set totrue, disables automatic body decompression (decompress_bodystep) and decoding (decode_bodystep). Defaults tofalse.:decode_body- if set tofalse, disables automatic response body decoding. Defaults totrue.:output- if set, writes the response body to a file (viaoutputstep). Can be set to a string path or an atom:remote_namewhich would use the remote name as the filename in the current working directory. Once the file is written, the response body is replaced with"".Setting
:outputalso sets thedecode_body: falseoption to prevent decoding the response before writing it to the file.
Response redirect options (follow_redirects step):
:follow_redirects- if set tofalse, disables automatic response redirects. Defaults totrue.:location_trusted- by default, authorization credentials are only sent on redirects with the same host, scheme and port. If:location_trustedis set totrue, credentials will be sent to any host.:max_redirects- the maximum number of redirects, defaults to10.
Retry options (retry step):
:retry: can be set to::safe(default) to only retry GET/HEAD requests on HTTP 408/5xx responses or exceptions,:alwaysto always retry,:neverto never retry, andfun- a 1-arity function that accepts either aReq.Responseor an exception struct and returns boolean whether to retry:retry_delay- sleep this number of milliseconds before making another attempt, defaults to2000:max_retries- maximum number of retry attempts, defaults to2(for a total of3requests to the server, including the initial one.)
Caching options (cache step):
:cache- iftrue, performs HTTP caching. Defaults tofalse.:cache_dir- the directory to store the cache, defaults to<user_cache_dir>/req(see::filename.basedir/3)
Request adapters:
:adapter- adapter to use to make the actual HTTP request. See:adapterfield description in theReq.Requestmodule documentation for more information. Defaults to callingrun_finch.:plug- if set, calls the given Plug instead of making an HTTP request over the network (viaput_plugstep).
Finch options (run_finch step)
:finch- the Finch pool to use. Defaults to pool automatically started byReq.:finch_options- options passed down to Finch when making the request, defaults to[]. SeeFinch.request/3for a list of available options.:http2- iftrue, uses an HTTP/2 pool automatically started by Req.:unix_socket- if set, connect through the given UNIX domain socket
examples
Examples
With options keywords list:
iex> {:ok, response} = Req.request(url: "https://api.github.com/repos/elixir-lang/elixir")
iex> response.status
200
iex> response.body["description"]
"Elixir is a dynamic, functional language designed for building scalable and maintainable applications"With request struct:
iex> req = Req.new(url: "https://api.github.com/repos/elixir-lang/elixir")
iex> {:ok, response} = Req.request(req)
iex> response.status
200With request struct and options:
iex> req = Req.new(base_url: "https://api.github.com")
iex> {:ok, response} = Req.request(req, url: "/repos/elixir-lang/elixir")
iex> response.status
200With mock adapter:
iex> adapter = fn request ->
...> response = %Req.Response{status: 200, body: "it works!"}
...> {request, response}
...> end
iex>
iex> {:ok, response} = Req.request(url: "http://example")
iex> response.body
"it works!"
@spec request(Req.Request.t(), options :: keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()}
Makes an HTTP request.
See request/1 for more information.