CurlReq.Request behaviour (CurlReq v0.100.1)

View Source

This struct is a general abstraction over an HTTP request of an HTTP client. It acts as an intermediate representation to convert from and into the desired formats.

Summary

Callbacks

decode from Elixir.CurlReq.Request to the destination type

encode from the custom type to Elixir.CurlReq.Request

Functions

Puts authorization into the CurlReq.Request struct

Puts the body and optional encoding into the CurlReq.Request struct It will immediately transform the input to the specified encoding when previously set.

Sets the compression option in the Curl.Request struct

Puts the cookie into the CurlReq.Request struct

Puts the body and optional encoding into the CurlReq.Request struct It will immediately transform the input to the specified encoding when previously set.

Puts the header into the CurlReq.Request struct. Special headers like encoding, authorization or user-agent are stored in their respective field in the Elixir.CurlReq.Request struct instead of a general header.

Sets the insecure option in the Curl.Request struct

Puts the method into the CurlReq.Request struct

Puts the proxy url into the CurlReq.Request struct, It either accepts a binary or an URI struct

Sets the redirect option in the Curl.Request struct

Puts the url into the CurlReq.Request struct, It either accepts a binary or an URI struct

Sets the user agent in the Curl.Request struct

Types

auth()

(since 0.100)
@type auth() :: {auth_option(), String.t()} | auth_option()

auth_option()

(since 0.100)
@type auth_option() :: :none | :basic | :bearer | :netrc

compression()

(since 0.100)
@type compression() :: :none | :gzip | :br | :zstd

cookie()

(since 0.100)
@type cookie() :: %{optional(String.t()) => String.t()}

encoding()

(since 0.100)
@type encoding() :: :raw | :form | :json

header()

(since 0.100)
@type header() :: %{optional(String.t()) => [String.t()]}

method()

(since 0.100)
@type method() :: :get | :head | :put | :post | :delete | :patch

protocols()

(since 0.100)
@type protocols() :: :http1_0 | :http1_1 | :http2

t()

(since 0.100)
@type t() :: %CurlReq.Request{
  auth: auth(),
  body: term(),
  compression: compression(),
  cookies: cookie(),
  encoding: encoding(),
  headers: header(),
  insecure: boolean(),
  method: method(),
  protocols: protocols(),
  proxy: boolean(),
  proxy_auth: auth(),
  proxy_url: term(),
  raw_body: String.t(),
  redirect: boolean(),
  url: URI.t(),
  user_agent: user_agent()
}

user_agent()

(since 0.100)
@type user_agent() :: :curl | :req | String.t()

Callbacks

decode(t, opts)

(since 0.100)
@callback decode(t(), opts :: Keyword.t()) :: term()

decode from Elixir.CurlReq.Request to the destination type

encode(source, opts)

(since 0.100)
@callback encode(source :: term(), opts :: Keyword.t()) :: t()

encode from the custom type to Elixir.CurlReq.Request

Functions

put_auth(request, arg2)

(since 0.100)
@spec put_auth(t(), {:bearer | :basic | :netrc, String.t()} | :netrc | nil) :: t()

Puts authorization into the CurlReq.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_auth({:basic, "barbaz"})
iex> request.auth
{:basic, "barbaz"}

put_body(request, input)

(since 0.100)
@spec put_body(t(), term()) :: t()

Puts the body and optional encoding into the CurlReq.Request struct It will immediately transform the input to the specified encoding when previously set.

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_body("some body")
iex> request.encoding
:raw
iex> request.body
"some body"

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_body(%{some: "body"})
iex> request.raw_body
%{some: "body"}

iex> request = %CurlReq.Request{} 
...> |> CurlReq.Request.put_encoding(:json) 
...> |> CurlReq.Request.put_body(~S|{"some": "body"}|)
iex> request.body
%{"some" => "body"}

put_compression(request, type)

(since 0.100)
@spec put_compression(t(), compression() | boolean() | nil) :: t()

Sets the compression option in the Curl.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_compression(true)
iex> request.compression
:gzip

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_compression(:br)
iex> request.compression
:br

put_cookie(request, key, value)

(since 0.100)
@spec put_cookie(t(), String.t(), String.t()) :: t()

Puts the cookie into the CurlReq.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_cookie("key1", "value1")
iex> request.cookies
%{"key1" => "value1"}

put_encoding(request, encoding)

(since 0.100)
@spec put_encoding(t(), encoding()) :: t()

Puts the body and optional encoding into the CurlReq.Request struct It will immediately transform the input to the specified encoding when previously set.

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_encoding(:json)
iex> request.encoding
:json

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_encoding(:form)
iex> request.encoding
:form

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_encoding(:raw)
iex> request.encoding
:raw

iex> request = %CurlReq.Request{} 
...> |> CurlReq.Request.put_body("foo=bar") 
...> |> CurlReq.Request.put_encoding(:form)
iex> request.body
%{"foo" =>  "bar"}

iex> request = %CurlReq.Request{} 
...> |> CurlReq.Request.put_body(~S|{"foo": "bar"}|) 
...> |> CurlReq.Request.put_encoding(:json)
iex> request.body
%{"foo" =>  "bar"}

put_header(request, key, val)

(since 0.100)
@spec put_header(t(), String.t(), String.t() | [String.t()]) :: t()

Puts the header into the CurlReq.Request struct. Special headers like encoding, authorization or user-agent are stored in their respective field in the Elixir.CurlReq.Request struct instead of a general header.

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_header("X-GitHub-Api-Version", "2022-11-28")
iex> request.headers
%{"x-github-api-version" => ["2022-11-28"]}

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_header("Content-Type", "application/json")
iex> request.encoding
:json

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_header("Authorization", "Bearer foobar")
iex> request.auth
{:bearer, "foobar"}

put_insecure(request, insecure)

(since 0.100)
@spec put_insecure(t(), boolean() | nil) :: t()

Sets the insecure option in the Curl.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_insecure(true)
iex> request.insecure
true

put_method(request, method)

(since 0.100)
@spec put_method(t(), method() | String.t()) :: t()

Puts the method into the CurlReq.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_method("PUT")
iex> request.method
:put

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_method(:post)
iex> request.method
:post

put_protocols(request, protocols)

(since 0.100)
@spec put_protocols(t(), [protocols()]) :: t()

put_proxy(request, uri)

(since 0.100)
@spec put_proxy(t(), URI.t() | String.t()) :: t()

Puts the proxy url into the CurlReq.Request struct, It either accepts a binary or an URI struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_proxy("https://example.com")
iex> request.proxy_url
URI.parse("https://example.com")

put_redirect(request, enabled)

(since 0.100)
@spec put_redirect(t(), boolean() | nil) :: t()
@spec put_redirect(t(), :req | :curl | String.t()) :: t()

Sets the redirect option in the Curl.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_redirect(true)
iex> request.redirect
true

put_url(request, uri)

(since 0.100)
@spec put_url(t(), URI.t() | String.t()) :: t()

Puts the url into the CurlReq.Request struct, It either accepts a binary or an URI struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_url("https://example.com")
iex> request.url
URI.parse("https://example.com")

put_user_agent(request, user_agent)

(since 0.100)

Sets the user agent in the Curl.Request struct

Examples

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_user_agent(:req)
iex> request.user_agent
:req

iex> request = %CurlReq.Request{} |> CurlReq.Request.put_user_agent("some user agent")
iex> request.user_agent
"some user agent"