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:
conn.private[:pgrest_raw_body], cached byPgRest.BodyReader.raw_bodyis those bytes andbodytheir decoded JSON.conn.body_paramsfromPlug.Parserswithout the reader. An array payload (%{"_json" => [...]}) is unwrapped, a scalar becomes[], an object is used as is, andraw_bodyisnil. Plug.Parsers' own:lengthgoverns 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 scalar5and also becomes[]; usePgRest.BodyReader(path 1, which decodes the raw bytes) to avoid that.- An unparsed body, read with
Plug.Conn.read_body/2.raw_bodykeeps 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?}fromprofile/2.:tx_end-:commit(default) ignoresPrefer: tx=...;:commit_allow_overridehonors it.:max_body_bytes- body size limit (default8_000_000); larger bodies are a 413.:rpc_body- for aPOST /rpc/<fn>::jsonreads an empty body as{}(a call without arguments);:rawkeeps a non-JSON body (text/plain,text/xml,application/octet-stream) undecoded inraw_body, withbodynil. Such a body needs the raw bytes (PgRest.BodyReader, or noPlug.Parsersfor it).
Differences from PostgREST
Strict invalid preferences return PGRST122 before select planning. When both are invalid, PostgREST v14.18 reports the select error first.
Summary
Functions
The body bytes of a request: raw_body when it was cached or read, else the decoded body
encoded once with Jason.encode!/1.
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).
The base of a media type header value, lowercased: its parameters (; charset=...,
; q=...) and surrounding whitespace dropped.
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.
Sets the request's claims, db_role and authenticated? from a PgRest.Auth resolution.
Types
@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 }
Functions
The body bytes of a request: raw_body when it was cached or read, else the decoded body
encoded once with Jason.encode!/1.
@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).
The base of a media type header value, lowercased: its parameters (; charset=...,
; q=...) and surrounding whitespace dropped.
@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.
@spec put_auth(t(), PgRest.Auth.t()) :: t()
Sets the request's claims, db_role and authenticated? from a PgRest.Auth resolution.