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

Copy Markdown View Source

Expect that response matches the given status.

This step ensures the HTTP response has the given expected status, otherwise it returns Req.UnexpectedStatusError.

Request Options

  • :expect - the expected HTTP response status. Can be one of the following:

    • integer
    • range
    • atom - one of :informational (1xx), :successful (2xx), :redirection (3xx), :client_error (4xx), or :server_error (5xx)
    • list of integers/ranges/atoms

Order Matters!

By default, Req.Expect runs AFTER Req.Retry, Req.Redirect, Req.Decompress, and Req.Decode steps.

This means that, for example, HTTP 503 error would be first retried, HTTP 307 redirect would be first followed, and the response body would be first decompressed and decoded before checking for expected HTTP status. If this is undesirable, re-arrange or disable and manually run given steps.

Examples

iex> resp = Req.get!("https://httpbingo.org/status/200", expect: 200)
iex> resp.status
200

iex> Req.get!("https://httpbingo.org/status/404", expect: :successful)
** (Req.UnexpectedStatusError) expected response status :successful (200..299), got: 404