Curling is a Go library that converts http.Request objects into cURL commands
go get -u github.com/aoliveti/curling
The following Go code demonstrates how to create a command from an HTTP request object:
package main
import (
"fmt"
"log"
"net/http"
"github.com/aoliveti/curling"
)
func main() {
req, err := http.NewRequest(http.MethodGet, "https://www.google.com", nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("If-None-Match", "foo")
cmd, err := curling.NewFromRequest(req)
if err != nil {
log.Fatal(err)
}
fmt.Println(cmd)
}
curl -X 'GET' 'https://www.google.com' -H 'If-None-Match: foo'
func NewFromRequest(r *http.Request, opts ...Option) (*Command, error) {
...
}
When creating a new command, you can provide these options:
Option | Description |
---|---|
WithLongForm() | Enables the long form for cURL options |
WithFollowRedirects() | Sets the flag -L, --location |
WithInsecure() | Sets the flag -k, --insecure |
WithSilent() | Sets the flag -s, --silent |
WithCompressed() | Sets the flag --compressed |
WithMultiLine() | Generates a multiline snippet for unix-like shell |
WithWindowsMultiLine() | Generates a multiline snippet for Windows shell |
WithPowerShellMultiLine() | Generates a multiline snippet for PowerShell |
WithDoubleQuotes() | Uses double quotes to escape characters |
WithRequestTimeout(seconds int) | Sets the flag -m, --max-time |
The library is released under the MIT license. See LICENSE file.