# `Stampede.HTTP`
[🔗](https://github.com/mandar1045/stampede/blob/v0.2.0/lib/stampede/http.ex#L1)

An HTTP client for load tests.

    {:ok, %{status: 200, body: body}} = Stampede.HTTP.get("http://localhost:4000/api/posts")
    {:ok, %{status: 201}} = Stampede.HTTP.post("http://localhost:4000/api/posts", json: %{title: "Hi"})

Responses are maps with the `:status`, the `:headers`, with lowercase
names, and the `:body`.

Each VU keeps a connection open to every server it talks to and sends its
next request on it, as a browser does. When the server closed it in the
meantime, a new one is opened, and a request that is safe to repeat (GET,
HEAD, PUT, DELETE or OPTIONS) is sent again on it.

## Metrics

Inside a run, requests record:

  * `"http.request"` - how long each response took, whatever its status
  * `"http.request.errors"` - requests that failed, or whose response had a
    status of 400 or more

# `response`

```elixir
@type response() :: %{
  status: non_neg_integer(),
  headers: [{String.t(), String.t()}],
  body: binary()
}
```

# `delete`

```elixir
@spec delete(String.t(), keyword()) :: {:ok, response()} | {:error, term()}
```

Sends a DELETE request. See `request/3`.

# `get`

```elixir
@spec get(String.t(), keyword()) :: {:ok, response()} | {:error, term()}
```

Sends a GET request. See `request/3`.

# `patch`

```elixir
@spec patch(String.t(), keyword()) :: {:ok, response()} | {:error, term()}
```

Sends a PATCH request. See `request/3`.

# `post`

```elixir
@spec post(String.t(), keyword()) :: {:ok, response()} | {:error, term()}
```

Sends a POST request. See `request/3`.

# `put`

```elixir
@spec put(String.t(), keyword()) :: {:ok, response()} | {:error, term()}
```

Sends a PUT request. See `request/3`.

# `request`

```elixir
@spec request(atom(), String.t(), keyword()) :: {:ok, response()} | {:error, term()}
```

Sends a `method` request to `url` and waits for the whole response.

Returns `{:ok, response}` whatever the status, and `{:error, reason}` when
there is no response, such as a `Mint.TransportError`.

## Options

  * `:headers` - a list of `{name, value}` headers
  * `:body` - the body, as iodata
  * `:json` - a term to send as JSON instead of a `:body`, with a
    `content-type` of `application/json` unless one is given
  * `:timeout` - how long to wait for the response, in milliseconds.
    Defaults to `5_000`.
  * `:metric` - the timing to record the request as, or `nil` to record
    nothing. Defaults to `"http.request"`.
  * `:transport_opts` - options for `:gen_tcp` or `:ssl`, given to
    `Mint.HTTP.connect/4`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
