http2curl

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

README

http2curl

convert Request of fasthttp, hertz and net/http to CURL command line and fork from moul/http2curl

Install

go get github.com/li-jin-gou/http2curl

Usage

FastHttp
func FastHttpDemo() {
	// fasthttp
	var req fasthttp.Request
	req.SetRequestURI("https://example.com/index")
	req.Header.SetMethod(fasthttp.MethodPost)
	req.SetBody([]byte(`{"a":"b"}`))
	req.Header.Set("Content-Type", "application/json")

	c, _ := http2curl.GetCurlCommandFastHttp(&req)
	fmt.Println(c)
	// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed
}

Hertz
func HertzDemo() {
	// hertz
	req := protocol.NewRequest(consts.MethodGet, "https://example.com/index", nil)
	req.URI().QueryArgs().Add("a", "1")
	req.URI().QueryArgs().Add("b", "2")
	req.Header.Set("a", "2")
	c, _ := http2curl.GetCurlCommandHertz(req)
	fmt.Println(c)
	// Output: curl -k -X 'GET' -H 'A: 2' -H 'Host: example.com' 'https://example.com/index?a=1&b=2' --compressed
}
net/http
func NetHttpDemo() {
	req, _ := http.NewRequest(http.MethodPost, "https://example.com/index", bytes.NewBufferString(`{"a":"b"}`))
	req.Header.Set("Content-Type", "application/json")
	c, _ := http2curl.GetCurlCommand(req)
	fmt.Println(c)
	// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Content-Type: application/json' 'https://example.com/index' --compressed
}
Resty
func RestyDemo() {
	var req *resty.Request
	client := resty.New()
	resp, _ := client.R().
		SetHeader("Content-Type", "application/json").
		SetBody([]byte(`{"a":"b"}`)).
		Post("https://example.com/index")
	req = resp.Request
	c, _ := http2curl.GetCurlCommandResty(req)
	fmt.Println(c)
	// Output: curl -k -X 'POST' -d '{"a":"b"}' -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'User-Agent: go-resty/2.12.0 (https://github.com/go-resty/resty)' 'https://example.com/index' --compressed
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorURINull = errors.New("getCurlCommand: invalid request, req.URL is nil")

Functions

This section is empty.

Types

type CurlCommand

type CurlCommand []string

CurlCommand contains exec.Command compatible slice + helpers

func GetCurlCommand added in v0.1.1

func GetCurlCommand(req *http.Request) (*CurlCommand, error)

GetCurlCommand returns a CurlCommand corresponding to an http.Request

func GetCurlCommandFastHttp

func GetCurlCommandFastHttp(req *fasthttp.Request) (*CurlCommand, error)

GetCurlCommandFastHttp returns a CurlCommand corresponding to an fasthttp.Request

func GetCurlCommandHertz

func GetCurlCommandHertz(req *protocol.Request) (*CurlCommand, error)

GetCurlCommandHertz returns a CurlCommand corresponding to an protocol.Request

func GetCurlCommandResty added in v0.1.2

func GetCurlCommandResty(req *resty.Request) (*CurlCommand, error)

func (*CurlCommand) String

func (c *CurlCommand) String() string

String returns a ready to copy/paste command

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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