# `PgRest.Request`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/request.ex#L1)

A parsed HTTP request: ordered params, `Prefer`, `Range` and the JSON body.

## Body sources

`from_conn/2` takes the body from the first of these that applies:

  1. `conn.private[:pgrest_raw_body]`, cached by `PgRest.BodyReader`. `raw_body`
     is those bytes and `body` their decoded JSON.
  2. `conn.body_params` from `Plug.Parsers` without the reader. An array payload
     (`%{"_json" => [...]}`) is unwrapped, a scalar becomes `[]`, an object is
     used as is, and `raw_body` is `nil`. Plug.Parsers' own `:length` governs the
     size limit on this path, not `:max_body_bytes`. Plug.Parsers wraps every
     non-object payload under `"_json"`, so here a genuine object body
     `{"_json": 5}` is indistinguishable from the scalar `5` and also becomes
     `[]`; use `PgRest.BodyReader` (path 1, which decodes the raw bytes) to
     avoid that.
  3. An unparsed body, read with `Plug.Conn.read_body/2`. `raw_body` keeps the bytes.

Only POST, PUT and PATCH decode a body; for other methods `body` is `nil` (a
cached or read `raw_body` is kept). A scalar JSON payload (`5`, `"x"`, `null`)
becomes an empty array, as in PostgREST; empty or invalid JSON is a 400 `PGRST102`.

## Schema profile

`PgRest.Plug` resolves the profile once (`profile/2`) and passes it as the `:profile` option:
`schema` is the schema the request targets and `profile_negotiated?` tells the responder to
send `Content-Profile`. Without the option, `schema` is `nil`.

## Options

  * `:profile` - `{schema, negotiated?}` from `profile/2`.
  * `:tx_end` - `:commit` (default) ignores `Prefer: tx=...`;
    `:commit_allow_override` honors it.
  * `:max_body_bytes` - body size limit (default `8_000_000`); larger bodies are
    a 413.
  * `:rpc_body` - for a `POST /rpc/<fn>`: `:json` reads an empty body as `{}` (a call
    without arguments); `:raw` keeps a non-JSON body (`text/plain`, `text/xml`,
    `application/octet-stream`) undecoded in `raw_body`, with `body` `nil`. Such a body
    needs the raw bytes (`PgRest.BodyReader`, or no `Plug.Parsers` for it).

## Differences from PostgREST

Strict invalid preferences return PGRST122 before select planning. When both are
invalid, PostgREST v14.18 reports the select error first.

# `t`

```elixir
@type t() :: %PgRest.Request{
  accepts: [PgRest.MediaType.t()],
  authenticated?: boolean(),
  authorization_state: PgRest.Authorization.State.t(),
  body: term(),
  claims: map(),
  conn: Plug.Conn.t(),
  db_role: String.t() | nil,
  method: String.t(),
  params: [{String.t(), String.t() | nil}],
  path: [String.t()],
  prefer: PgRest.Prefer.t(),
  profile_negotiated?: boolean(),
  range: PgRest.Request.Range.t(),
  raw_body: binary() | nil,
  schema: String.t() | nil
}
```

# `body_bytes`

```elixir
@spec body_bytes(t()) :: binary()
```

The body bytes of a request: `raw_body` when it was cached or read, else the decoded `body`
encoded once with `Jason.encode!/1`.

# `from_conn`

```elixir
@spec from_conn(Plug.Conn.t(), keyword() | map()) ::
  {:ok, t()} | {:error, PgRest.Error.t()}
```

Builds a request from a `Plug.Conn`. `claims`, `db_role` and `authenticated?` come from the
`PgRest.Auth` resolution `PgRest.Plug` stores in `conn.private[:pgrest_auth]` (anonymous
without one).

# `media_base`

```elixir
@spec media_base(String.t()) :: String.t()
```

The base of a media type header value, lowercased: its parameters (`; charset=...`,
`; q=...`) and surrounding whitespace dropped.

# `profile`

```elixir
@spec profile(Plug.Conn.t(), PgRest.Registry.Snapshot.t()) ::
  {:ok, {String.t(), boolean()}} | {:error, PgRest.Error.t()}
```

The schema `conn` targets in `snapshot`: `Content-Profile` for POST, PATCH, PUT and DELETE, the
`Accept-Profile` header for every other method (GET, HEAD, OPTIONS, ...), and the default schema
without one. A schema that is not exposed is PostgREST's 406 `PGRST106`. Reads headers only.

`negotiated?` follows PostgREST's `iNegotiatedByProfile`: the schema counts as negotiated (and
the response carries `Content-Profile`) when a profile header named it or several schemas are
exposed.

# `put_auth`

```elixir
@spec put_auth(t(), PgRest.Auth.t()) :: t()
```

Sets the request's `claims`, `db_role` and `authenticated?` from a `PgRest.Auth` resolution.

---

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