fasthttp2curl

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 3 Imported by: 0

README

fasthttp2curl

Convert fasthttp.Request to CURL command line

Example

import (
	"fmt"

	"github.com/valyala/fasthttp"
	"github.com/andot/fasthttp2curl"
)

req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPut)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.SetBodyString(`{"hello":"world","answer":42}`)
req.Header.SetContentType("application/json")
command, _ := GetCurlCommand(req)
fasthttp.ReleaseRequest(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 github.com/andot/fasthttp2curl

License

© 2022 Ma Bingyao

Licensed under the MIT license (LICENSE-MIT).

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCurlCommand

func GetCurlCommand(req *fasthttp.Request) string
Example
form := url.Values{}
form.Add("age", "10")
form.Add("name", "Hudson")
body := form.Encode()
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPost)
req.SetRequestURI("http://foo.com/cats")
req.SetBodyString(body)
req.Header.Set("API_KEY", "123")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(req)
fmt.Println(command)
Output:

curl -X 'POST' -d 'age=10&name=Hudson' -H 'Api_key: 123' 'http://foo.com/cats'
Example (EmptyStringBody)
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPut)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.Header.SetContentType("application/json")
req.SetBodyString("")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(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)
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPut)
req.SetRequestURI("https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.Header.SetContentType("application/json")
req.SetBodyString(`{"hello":"world","answer":42}`)
req.Header.Set("X-Auth-Token", "private-token")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(req)
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 := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPut)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.SetBodyString(`{"hello":"world","answer":42}`)
req.Header.SetContentType("application/json")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(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 := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPost)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.Header.SetContentType("application/json")
req.SetBodyString("hello\nworld")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(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 := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPut)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.Header.SetContentType("application/json")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(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)
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPut)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.Header.SetContentType("application/json")
req.SetBodyString(`{"hello":"world","answer":42}`)
req.Header.Set("X-Auth-Token", "private-token")
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(req)
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 (SpecialCharsInBody)
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPost)
req.SetRequestURI("http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu")
req.Header.SetContentType("application/json")
req.SetBodyString(`Hello $123 o'neill -"-`)
command := GetCurlCommand(req)
fasthttp.ReleaseRequest(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'

Types

This section is empty.

Jump to

Keyboard shortcuts

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