curl2go

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 5 Imported by: 0

README

curl2go

Convert curl to golang code. Inspired By mholt/curl-to-go

Usage:

echo "curl -X POST https://reqbin.com/echo/post/json                                                                                  main ✚ ◼
-H "Content-Type: application/json"
-d '{"productId": 123456, "quantity": 100}' " | ./curl2go

input:

curl -X POST https://reqbin.com/echo/post/json                                                                                  main ✚ ◼
        -H "Content-Type: application/json"
        -d '{"productId": 123456, "quantity": 100}'

output:

package main

import (
        "fmt"
        "log"
        "time"
        "net/http"
        "net/http/httputil"
)

func init() {
    log.SetFlags(log.Lshortfile | log.Ltime)
}

func main() {
        client := http.Client{
                Transport: &http.Transport{
                        Proxy:                 nil,
                        TLSHandshakeTimeout:   time.Second * 5,
                        DisableKeepAlives:     true,
                        ResponseHeaderTimeout: time.Second * time.Duration(10),
                },
                CheckRedirect: func(req *http.Request, via []*http.Request) error {
                        return http.ErrUseLastResponse
                },
                Timeout: time.Second * time.Duration(10),
        }

        
        r, _ := http.NewRequest("POST", "https://reqbin.com/echo/post/json", nil)
        

        resp, err := client.Do(r)
        if err != nil {
                log.Fatal("SendRequest", err.Error())
        }
        
        defer resp.Body.Close()
        data, err := httputil.DumpResponse(resp, true)
        if err != nil {
                log.Fatal("DumpResponse", err)
        }

        fmt.Println(string(data))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BoolOptions = map[string]bool{}/* 226 elements not displayed */
View Source
var OptionAliases = map[string]string{
	"0": "http1.0",
	"1": "tlsv1",
	"2": "sslv2",
	"3": "sslv3",
	"4": "ipv4",
	"6": "ipv6",
	"a": "append",
	"A": "user-agent",
	"b": "cookie",
	"B": "use-ascii",
	"c": "cookie-jar",
	"C": "continue-at",
	"d": "data",
	"D": "dump-header",
	"e": "referer",
	"E": "cert",
	"f": "fail",
	"F": "form",
	"g": "globoff",
	"G": "get",
	"h": "help",
	"H": "header",
	"i": "include",
	"I": "head",
	"j": "junk-session-cookies",
	"J": "remote-header-name",
	"k": "insecure",
	"K": "config",
	"l": "list-only",
	"L": "location",
	"m": "max-time",
	"M": "manual",
	"n": "netrc",
	"N": "no-buffer",
	"o": "output",
	"O": "remote-name",
	"p": "proxytunnel",
	"P": "ftp-port",
	"q": "disable",
	"Q": "quote",
	"r": "range",
	"R": "remote-time",
	"s": "silent",
	"S": "show-error",
	"t": "telnet-option",
	"T": "upload-file",
	"u": "user",
	"U": "proxy-user",
	"v": "verbose",
	"V": "version",
	"w": "write-out",
	"x": "proxy",
	"X": "request",
	"Y": "speed-limit",
	"y": "speed-time",
	"z": "time-cond",
	"Z": "parallel",
	"#": "progress-bar",
	":": "next",
}

Functions

func Curl2go

func Curl2go(curl string) (string, error)

func ParseHeaders

func ParseHeaders(stringHeaders []string) map[string]string

Types

type BasicAuth

type BasicAuth struct {
	User string
	Pass string
}

type ParsedFlags

type ParsedFlags struct {
	UnFlags      []string
	BoolFlags    map[string]bool
	StringsFlags map[string][]string
}

func FlagParse

func FlagParse(input string) ParsedFlags

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser

func NewParser() *Parser

func NewParserWithOptions

func NewParserWithOptions(options ParserOptions) *Parser

func (*Parser) FlagParse

func (p *Parser) FlagParse(input string) ParsedFlags

type ParserOptions

type ParserOptions struct {
	Alias map[string]string
	Bool  map[string]bool
}

type Proxy

type Proxy struct {
	Server string
	Auth   *BasicAuth
}

type Relevant

type Relevant struct {
	URL       string
	Method    string
	Headers   map[string]string
	Data      RelevantData
	DataType  string
	Insecure  bool
	BasicAuth *BasicAuth

	Host  string
	Proxy *Proxy
}

Relevant is some of the parameters related to constructing a Go request

func ExtractRelevant

func ExtractRelevant(flags ParsedFlags) (*Relevant, error)

ExtractRelevant parse and extract the relevant parameters from map to a struct

type RelevantData

type RelevantData struct {
	Ascii string
	Files []string
}

type Render

type Render struct {
	// contains filtered or unexported fields
}

func NewRender

func NewRender() *Render

func (*Render) Curl2Go

func (rd *Render) Curl2Go(curl string) (string, error)

func (*Render) Render

func (rd *Render) Render(r *Relevant) (string, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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