Skip to content

li-jin-gou/http2curl

Repository files navigation

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
}