http2curl

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: Apache-2.0, MIT Imports: 6 Imported by: 39

README

http2curl

📐 Convert Golang's http.Request to CURL command line

go.dev reference License GitHub release Docker Metrics Made by Manfred Touron

Go Release PR GolangCI codecov Go Report Card CodeFactor

To do the reverse operation, check out mholt/curl-to-go.

Example

import (
    "http"
    "moul.io/http2curl"
)

data := bytes.NewBufferString(`{"hello":"world","answer":42}`)
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", data)
req.Header.Set("Content-Type", "application/json")

command, _ := http2curl.GetCurlCommand(req)
fmt.Println(command)
// Output: curl -X PUT -d "{\"hello\":\"world\",\"answer\":42}" -H "Content-Type: application/json" http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu

Install

go get moul.io/http2curl

Usages

License

© 2019-2021 Manfred Touron

Licensed under the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option. See the COPYRIGHT file for more details.

SPDX-License-Identifier: (Apache-2.0 OR MIT)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CurlCommand

type CurlCommand []string

CurlCommand contains exec.Command compatible slice + helpers

func GetCurlCommand

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

GetCurlCommand returns a CurlCommand corresponding to an http.Request

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

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

command, _ := GetCurlCommand(req)
fmt.Println(command)
Output:

curl -X 'POST' -d 'age=10&name=Hudson' -H 'Api_key: 123' 'http://foo.com/cats'
Example (EmptyStringBody)
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(""))
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(command)
Output:

curl -X 'PUT' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (Https)
uri := "https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu"
payload := new(bytes.Buffer)
payload.Write([]byte(`{"hello":"world","answer":42}`))
req, err := http.NewRequest("PUT", uri, payload)
if err != nil {
	panic(err)
}
req.Header.Set("X-Auth-Token", "private-token")
req.Header.Set("Content-Type", "application/json")

command, err := GetCurlCommand(req)
if err != nil {
	panic(err)
}
fmt.Println(command)
Output:

curl -k -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (Json)
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`))
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(command)
Output:

curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (NewlineInBody)
req, _ := http.NewRequest("POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString("hello\nworld"))
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(command)
Output:

curl -X 'POST' -d 'hello
world' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (NoBody)
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", nil)
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(command)
Output:

curl -X 'PUT' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (Other)
uri := "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu"
payload := new(bytes.Buffer)
payload.Write([]byte(`{"hello":"world","answer":42}`))
req, err := http.NewRequest("PUT", uri, payload)
if err != nil {
	panic(err)
}
req.Header.Set("X-Auth-Token", "private-token")
req.Header.Set("Content-Type", "application/json")

command, err := GetCurlCommand(req)
if err != nil {
	panic(err)
}
fmt.Println(command)
Output:

curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (Slice)
// See https://github.com/moul/http2curl/issues/12
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`{"hello":"world","answer":42}`))
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(strings.Join(*command, " \\\n  "))
Output:

curl \
  -X \
  'PUT' \
  -d \
  '{"hello":"world","answer":42}' \
  -H \
  'Content-Type: application/json' \
  'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
Example (SpecialCharsInBody)
req, _ := http.NewRequest("POST", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString(`Hello $123 o'neill -"-`))
req.Header.Set("Content-Type", "application/json")

command, _ := GetCurlCommand(req)
fmt.Println(command)
Output:

curl -X 'POST' -d 'Hello $123 o'\''neill -"-' -H 'Content-Type: application/json' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'

func (*CurlCommand) String

func (c *CurlCommand) String() string

String returns a ready to copy/paste command

Jump to

Keyboard shortcuts

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