PhoenixGenApi.Structs.Request (PhoenixGenApi v2.19.0)

Copy Markdown View Source

Request struct for internal using, convert data map from websocket api.

Data from websocket api has payload like this:

%{
  "request_id" => "request_id",
  "request_type" => "request_type",
  "service" => "service",
  "user_id" => "user_id",
  "device_id" => "device_id",
  "args" => %{}
}

We need to convert it to struct for internal using.

Like this:

%PhoenixGenApi.Structs.Request{
  request_id: "request_id",
  request_type: "request_type",
  service: "service",
  user_id: "user_id",
  device_id: "device_id",
  args: %{}
}

Explain:

  • user_id: string, user's id in system. User's id in system. It need to check permission.

  • device_id: string, device id of current connection. Device id of current connection.

  • request_type: string, request type. Request type. Using for identify function to call in system.

  • request_id: string, unique id for request. Make by client. Unique id for request. Make by client. Using for identify response.

  • service: string, service name. Service name. Using for identify service to call in system.

  • args: map, field -> value, arguments for request. Arguments for request. Using for call function in system.

Payload Size Validation

decode!/1 validates the payload size before deserialization to prevent memory exhaustion from oversized requests. The limit is configurable via application env:

config :phoenix_gen_api, :request,
  max_payload_bytes: 1_000_000  # default: 1MB

Use max_payload_bytes/0 to read the current configured limit at runtime.

Security Considerations

  • Payload size is checked before deserialization to prevent memory exhaustion attacks where a client sends an enormous map.
  • The default limit is 1MB and can be configured per application needs via config :phoenix_gen_api, :request, max_payload_bytes: N.
  • The check uses :erlang.external_size/1 (when available) for accurate measurement without allocating the full binary. Falls back to byte_size(:erlang.term_to_binary(params)) on older Erlang/OTP releases.
  • If the payload exceeds the limit, decode!/1 raises an error with the configured maximum and the actual measured size.

Summary

Types

t()

Request struct for internal using, convert data map from websocket api.

Functions

Create Request from params for convert data map from websocket api.

Returns the configured maximum payload size in bytes.

Types

t()

@type t() :: %PhoenixGenApi.Structs.Request{
  args: map(),
  device_id: String.t() | nil,
  request_id: String.t(),
  request_type: String.t(),
  service: String.t(),
  user_id: String.t() | nil,
  user_roles: [String.t()] | nil,
  version: String.t() | nil
}

Request struct for internal using, convert data map from websocket api.

Functions

decode!(params)

Create Request from params for convert data map from websocket api.

Validates payload size before deserialization. Raises if the payload exceeds the configured max_payload_bytes limit.

max_payload_bytes()

Returns the configured maximum payload size in bytes.

Reads from config :phoenix_gen_api, :request, max_payload_bytes. Defaults to 1MB (1000000 bytes).