curling

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 7 Imported by: 1

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	// contains filtered or unexported fields
}

A Command represents a cURL command based on an HTTP request.

Returned by NewFromRequest, a Command expects a pointer to http.Request as input. It generates a cURL command string, which by default uses a single line command, short form options, and single quote escaping.

func NewFromRequest

func NewFromRequest(r *http.Request, opts ...Option) (*Command, error)

NewFromRequest returns a new Command that reads from r. If the request has an invalid URL, NewFromRequest returns an error. If NewFromRequest can't read the request body, it returns an error.

func (*Command) String

func (c *Command) String() string

String returns the cURL command.

type Option

type Option func(curling *Command)

func WithCompression

func WithCompression() Option

WithCompression enables the option --compressed.

func WithDoubleQuotes added in v1.0.4

func WithDoubleQuotes() Option

WithDoubleQuotes enables escaping using double quotes.

func WithFollowRedirects

func WithFollowRedirects() Option

WithFollowRedirects enables the option -L, --location.

func WithInsecure

func WithInsecure() Option

WithInsecure enables the option -k, --insecure.

func WithLongForm

func WithLongForm() Option

WithLongForm enables the long form for cURL options. Example: --header instead of -H.

func WithMultiLine

func WithMultiLine() Option

WithMultiLine splits the command across multiple lines. The default line continuation character is backslash.

func WithPowerShellMultiLine

func WithPowerShellMultiLine() Option

WithPowerShellMultiLine splits the command across multiple lines. The line continuation character is backtick.

func WithRequestTimeout added in v1.1.0

func WithRequestTimeout(seconds int) Option

WithRequestTimeout enables the option -m, --max-time. It sets the number of seconds the request should wait for a response before timing out. Negative value seconds will be silently ignored.

func WithSilent added in v1.0.3

func WithSilent() Option

WithSilent enables the option -s, --silent.

func WithWindowsMultiLine

func WithWindowsMultiLine() Option

WithWindowsMultiLine splits the command across multiple lines. The line continuation character is caret.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL