Tesla.Middleware.Curl (TeslaCurl v1.4.0)

View Source

A middleware for the Tesla HTTP client that logs requests expressed in Curl.

Parses Tesla.Env structs into a curl command and logs it. This is useful for debugging requests and responses.

Examples

defmodule MyClient do
  def middleware do
    [
      {Tesla.Middleware.Curl, follow_redirects: true, redact_fields: ["api_token", "authorization"]},
      Tesla.Middleware.JSON
    ]
  end

  def client do
    Tesla.client(middleware())
  end

  def get_user(id) do
    Tesla.get(client(), "/users/#{id}")
  end
end

Options

  • :follow_redirects - boolean, will add the -L flag to the curl command
  • :redact_fields - a list of keys or regex capture groups to redact from the request body
  • :compressed - boolean, will add the --compressed flag to the curl command
  • :logger_level - the level at which to log the curl command, as an atom. Must be one of - :emergency, :alert, :critical, :error, :warning, :notice, :info, :debug

Summary

Functions

Serves as the main entrypoint to the middleware. Handles this middleware and calls the next piece of middleware in the chain.

Calls the function to construct the curl command and logs it. If an error occurs, it will be logged, and the request will continue as normal. This can be used as a standalone function if you want to log a curl command without using the middleware.

Types

method()

@type method() :: :head | :get | :delete | :trace | :options | :post | :put | :patch

Functions

call(env, next, opts \\ [])

@spec call(Tesla.Env.t(), Tesla.Env.stack(), keyword() | nil) :: Tesla.Env.result()

Serves as the main entrypoint to the middleware. Handles this middleware and calls the next piece of middleware in the chain.

log(env, opts)

@spec log(Tesla.Env.t(), keyword() | nil) :: :ok

Calls the function to construct the curl command and logs it. If an error occurs, it will be logged, and the request will continue as normal. This can be used as a standalone function if you want to log a curl command without using the middleware.