CurlReq.Request behaviour (CurlReq v0.100.1)
View SourceThis 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
@type auth() :: {auth_option(), String.t()} | auth_option()
@type auth_option() :: :none | :basic | :bearer | :netrc
@type compression() :: :none | :gzip | :br | :zstd
@type encoding() :: :raw | :form | :json
@type method() :: :get | :head | :put | :post | :delete | :patch
@type protocols() :: :http1_0 | :http1_1 | :http2
@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() }
@type user_agent() :: :curl | :req | String.t()
Callbacks
Functions
Puts authorization into the CurlReq.Request struct
Examples
iex> request = %CurlReq.Request{} |> CurlReq.Request.put_auth({:basic, "barbaz"})
iex> request.auth
{:basic, "barbaz"}
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"}
@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
Puts the cookie into the CurlReq.Request struct
Examples
iex> request = %CurlReq.Request{} |> CurlReq.Request.put_cookie("key1", "value1")
iex> request.cookies
%{"key1" => "value1"}
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"}
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"}
Sets the insecure option in the Curl.Request struct
Examples
iex> request = %CurlReq.Request{} |> CurlReq.Request.put_insecure(true)
iex> request.insecure
true
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
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")
@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
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")
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"