Skip to content

Curling is a Go library that converts Go HTTP requests into cURL commands

License

Notifications You must be signed in to change notification settings

aoliveti/curling

Repository files navigation

curling

GitHub go.mod Go version GitHub Actions Workflow Status Go Reference codecov Go Report Card GitHub License

Curling is a Go library that converts http.Request objects into cURL commands

Install

go get -u github.com/aoliveti/curling

Usage

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'

Options

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

License

The library is released under the MIT license. See LICENSE file.