http2curl

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CurlCmd

type CurlCmd []string

CurlCmd contains exec.Cmd compatible slice + helpers

func GetCurlCmd

func GetCurlCmd(r *http.Request) (*CurlCmd, error)

GetCurlCmd returns a CurlCmd corresponding to an http.Request

Example
form := url.Values{}
form.Add("age", "10")
form.Add("name", "Hudson")
body := form.Encode()

payload := ioutil.NopCloser(bytes.NewBufferString(body))
req, _ := http.NewRequest(http.MethodPost, "http://foo.com/cats", payload)
req.Header.Set("Api-Key", "123")

cmd, _ := GetCurlCmd(req)
fmt.Println(cmd)
Output:
curl -X POST -d 'age=10&name=Hudson' -H 'Api-Key: 123' 'http://foo.com/cats'
Example (EmptyStringBody)
req, _ := http.NewRequest("PUT", "http://a.b.c/abc?jlk=mno&pqr=stu", bytes.NewBufferString(""))
cmd, _ := GetCurlCmd(req)
fmt.Println(cmd)
Output:
curl -X PUT 'http://a.b.c/abc?jlk=mno&pqr=stu'
Example (Json)
payload := bytes.NewBufferString(`{"hello":"world","answer":42}`)
req, _ := http.NewRequest("PUT", "http://a.b.c/abc?jlk=mno&pqr=stu", payload)
req.Header.Set("Content-Type", "application/json")

cmd, _ := GetCurlCmd(req)
fmt.Println(cmd)
Output:
curl -X PUT -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' 'http://a.b.c/abc?jlk=mno&pqr=stu'
Example (NewlineInBody)
req, _ := http.NewRequest("POST", "http://a.b.c/abc?jlk=mno&pqr=stu", bytes.NewBufferString("hello\nworld"))
cmd, _ := GetCurlCmd(req)
fmt.Println(cmd)
Output:
curl -X POST -d 'hello
world' 'http://a.b.c/abc?jlk=mno&pqr=stu'
Example (NoBody)
req, _ := http.NewRequest("PUT", "http://a.b.c/abc?jlk=mno&pqr=stu", nil)
cmd, _ := GetCurlCmd(req)
fmt.Println(cmd)
Output:
curl -X PUT 'http://a.b.c/abc?jlk=mno&pqr=stu'
Example (Other)
payload := bytes.NewBufferString(`{"hello":"world","answer":42}`)
req, err := http.NewRequest("PUT", "http://a.b.c/abc?jlk=mno&pqr=stu", payload)
if err != nil {
	panic(err)
}
req.Header.Set("X-Auth-Token", "private-token")
req.Header.Set("Content-Type", "application/json")

cmd, err := GetCurlCmd(req)
if err != nil {
	panic(err)
}
fmt.Println(cmd)
Output:
curl -X PUT -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'http://a.b.c/abc?jlk=mno&pqr=stu'
Example (Slice)
// See https://github.com/moul/http2curl/issues/12
payload := bytes.NewBufferString(`{"hello":"world","answer":42}`)
req, _ := http.NewRequest("PUT", "http://a.b.c/abc?jlk=mno&pqr=stu", payload)

cmd, _ := GetCurlCmd(req)
fmt.Println(cmd.Lines())
Output:
curl -X PUT \
  -d '{"hello":"world","answer":42}' \
  'http://a.b.c/abc?jlk=mno&pqr=stu'
Example (SpecialCharsInBody)
payload := bytes.NewBufferString(`Hello $123 o'neill -"-`)
req, _ := http.NewRequest("POST", "http://a.b.c/abc?jlk=mno&pqr=stu", payload)
cmd, _ := GetCurlCmd(req)
fmt.Println(cmd)
Output:
curl -X POST -d 'Hello $123 o'\''neill -"-' 'http://a.b.c/abc?jlk=mno&pqr=stu'

func (CurlCmd) Lines

func (c CurlCmd) Lines() string

Lines returns a ready to copy/paste cmd

func (CurlCmd) String

func (c CurlCmd) String() string

String returns a ready to copy/paste cmd

Jump to

Keyboard shortcuts

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