View Source CurlReq (CurlReq v0.98.4)
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.
Usage
Req to Curl
# 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!()
Curl to Req
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!()
Req Plugin
One final feature to note the Req plugin, CurlReq.Plugin
. Use CurlReq.Plugin.attach/2
to set up curl logging (inspired by TeslaCurl
).
iex> Req.new(url: "/fact", base_url: "https://catfact.ninja/")
...> |> CurlReq.Plugin.attach()
...> # |> Req.request!()
Summary
Functions
Transforms a curl command into a Req request.
Inspect a Req struct in curl syntax.
Same as from_curl/1
but as a sigil. The benefit here is, that the Req.Request struct will be created at compile time and you don't need to escape the string
Transforms a Req request into a curl command.
Types
@type inspect_opt() :: {:label, String.t()}
Functions
@spec from_curl(String.t()) :: Req.Request.t()
Transforms a curl command into a Req request.
Supported curl command line flags are supported:
-H
/--header
-X
/--request
-d
/--data
-b
/--cookie
-I
/--head
-F
/--form
-L
/--location
-u
/--user
The curl
command prefix is optional
Info
Only string inputs are supported. That means for example
-d @data.txt
will not load the file or-d @-
will not read from stdin
Examples
iex> CurlReq.from_curl("curl https://www.google.com")
%Req.Request{method: :get, url: URI.parse("https://www.google.com")}
iex> ~S(curl -d "some data" https://example.com) |> CurlReq.from_curl()
%Req.Request{method: :get, body: "some data", url: URI.parse("https://example.com")}
iex> CurlReq.from_curl("curl -I https://example.com")
%Req.Request{method: :head, url: URI.parse("https://example.com")}
iex> CurlReq.from_curl("curl -b cookie_key=cookie_val https://example.com")
%Req.Request{method: :get, headers: %{"cookie" => ["cookie_key=cookie_val"]}, url: URI.parse("https://example.com")}
@spec inspect(Req.Request.t(), [inspect_opt()]) :: Req.Request.t()
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!()
Same as from_curl/1
but as a sigil. The benefit here is, that the Req.Request struct will be created at compile time and you don't need to escape the string
Examples
iex> import CurlReq
...> ~CURL(curl "https://www.google.com")
%Req.Request{method: :get, url: URI.parse("https://www.google.com")}
iex> import CurlReq
...> ~CURL(curl -d "some data" "https://example.com")
%Req.Request{method: :get, body: "some data", url: URI.parse("https://example.com")}
iex> import CurlReq
...> ~CURL(curl -I "https://example.com")
%Req.Request{method: :head, url: URI.parse("https://example.com")}
iex> import CurlReq
...> ~CURL(curl -b "cookie_key=cookie_val" "https://example.com")
%Req.Request{method: :get, headers: %{"cookie" => ["cookie_key=cookie_val"]}, url: URI.parse("https://example.com")}
@spec to_curl(Req.Request.t(), Keyword.t()) :: String.t()
Transforms a Req request into a curl command.
Supported curl flags are:
-b
-H
-X
-I
-d
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)