View Source CurlReq (CurlReq v0.98.3)

Req is awesome, but the world speaks curl.

Next time you're debugging a 3rd party API and need to ask for support, you can just toss in this line:

|> CurlReq.inspect()

And you'll have the full curl command.

# Turn a Req request into a `curl` command.

iex> Req.new(url: "/fact", base_url: "https://catfact.ninja/")
...> |> CurlReq.to_curl()
"curl -H \"accept-encoding: gzip\" -H \"user-agent: req/0.4.14\" -X GET https://catfact.ninja/fact" 

# Or use `CurlReq.inspect/2` to inspect inline.

iex> Req.new(url: "/fact", base_url: "https://catfact.nijna/")
...> |> CurlReq.inspect(label: "MY REQ")
...> # |> Req.request!()

CurlReq also implements the ~CURL sigil, which converts a curl command to its corresponding Req request.

iex> import CurlReq
...> ~CURL(curl https://www.google.com)
...> # |> Req.request!()

Read the announcement here.

Installation

The package can be installed by adding curl_req to your list of dependencies in mix.exs:

def deps do
  [
    {:curl_req, "~> 0.98.0"}
  ]
end

The docs can be found at https://hexdocs.pm/curl_req.

Contributions

Contributions are welcome! There are gaps in the library, and this is open source, so let's work together to fill them!

  • [ ] ~CURL sigil handles newlines
  • [x] curl [url]
  • [x] curl -H
  • [x] curl -X
  • [x] curl -d
  • [x] curl -b
  • [x] curl long form options (--header, --data, etc)
  • [ ] Req Plugin to log curl command (like TeslaCurl)

How to contribute

  • Clone the repository to your computer.
  • Add to your mix.exs file in your project: {:curl_req, path: "~/path/to/curl_req"}.
  • Tinker until it does what you want.
  • Add a test covering your case.
  • Submit a PR!

Summary

Functions

Inspect a Req struct in curl syntax.

Transforms a curl command into a Req request.

Transforms a Req request into a curl command.

Types

@type inspect_opt() :: {:label, String.t()}
@type req_request() :: %Req.Request{
  adapter: term(),
  async: term(),
  body: term(),
  current_request_steps: term(),
  error_steps: term(),
  halted: term(),
  headers: term(),
  into: term(),
  method: term(),
  options: term(),
  private: term(),
  registered_options: term(),
  request_steps: term(),
  response_steps: term(),
  url: term()
}

Functions

Link to this function

inspect(req, opts \\ [])

View Source
@spec inspect(req_request(), [inspect_opt()]) :: req_request()

Inspect a Req struct in curl syntax.

Returns the unchanged req, just like IO.inspect/2.

Examples

iex> Req.new(url: URI.parse("https://www.google.com"))
...> |> CurlReq.inspect()
...> # |> Req.request!()
Link to this macro

sigil_CURL(arg, extra)

View Source (macro)

Transforms a curl command into a Req request.

Examples

iex> import CurlReq
...> ~CURL(curl "https://www.google.com")
%Req.Request{method: :get, url: URI.parse("https://www.google.com")}
@spec to_curl(req_request()) :: String.t()

Transforms a Req request into a curl command.

Examples

iex> Req.new(url: URI.parse("https://www.google.com"))
...> |> CurlReq.to_curl()
~S(curl -H "accept-encoding: gzip" -H "user-agent: req/0.4.14" -X GET https://www.google.com)