curlHelper

package
v0.0.0-...-08474b0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

README

Parse curl To golang requests

  • requests(https://github.com/474420502/requests)
  • Easy to transform curl bash to golang code
  • requests(inherit from curl bash) can add setting(config,cookie,header) and request url by you

Installation

  • To install the library, run the following command:
go get github.com/474420502/gcurl

Example

  • Example1: Basic GET request with headers

This example demonstrates how to parse a cURL command for a GET request with custom headers, create a session, and execute the request.

	surl := ` http://httpbin.org/get  -H 'Connection: keep-alive' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.9'`
	curl := gcurl.Parse(surl)
	ses := curl.CreateSession()
	tp := curl.CreateTemporary(ses)
	log.Println(ses.GetHeader())
	// map[Accept-Encoding:[gzip, deflate] Accept-Language:[zh-CN,zh;q=0.9] Connection:[keep-alive]]
	resp, err := tp.Execute()
	if err != nil {
		log.Panic(err)
	}

	log.Println(string(resp.Content()))
	//     ------response-----
	//     "args": {},
	//     "headers": {
	//     "Accept-Encoding": "gzip, deflate",
	//     "Accept-Language": "zh-CN,zh;q=0.9",
	//     "Connection": "keep-alive,close",
	//     "Host": "httpbin.org",
	//     "User-Agent": "Go-http-client/1.1"
	//     },
	//     "origin": "172.17.0.1",
	//     "url": "http://httpbin.org/get"
	// }
  • example2: GET request with cookies

This example demonstrates how to parse a cURL command for a GET request with custom headers and cookies, create a session, and execute the request.

	scurl := `curl 'http://httpbin.org/get' 
	--connect-timeout 1 
	-H 'authority: appgrowing.cn'
	-H 'accept-encoding: gzip, deflate, br' -H 'accept-language: zh' -H 'cookie: _ga=GA1.2.1371058419.1533104518; _gid=GA1.2.896241740.1543307916; _gat_gtag_UA_4002880_19=1' -H 'if-none-match: W/"5bf7a0a9-ca6"' -H 'if-modified-since: Fri, 23 Nov 2018 06:39:37 GMT'`
	curl := gcurl.Parse(scurl)
	ses := curl.CreateSession()
	wf := curl.CreateTemporary(ses)
	log.Println(ses.GetCookies(wf.ParsedURL))
	// [_ga=GA1.2.1371058419.1533104518 _gid=GA1.2.896241740.1543307916 _gat_gtag_UA_4002880_19=1]
	resp, err := wf.Execute()
	if err != nil {
		log.Panic(string(resp.Content()))
	}
	log.Println(string(resp.Content()))
	// {
	// 	"args": {},
	// 	"headers": {
	// 	  "Accept-Encoding": "gzip, deflate, br",
	// 	  "Accept-Language": "zh",
	// 	  "Authority": "appgrowing.cn",
	// 	  "Connection": "close",
	// 	  "Cookie": "_ga=GA1.2.1371058419.1533104518; _gid=GA1.2.896241740.1543307916; _gat_gtag_UA_4002880_19=1",
	// 	  "Host": "httpbin.org",
	// 	  "If-Modified-Since": "Fri, 23 Nov 2018 06:39:37 GMT",
	// 	  "If-None-Match": "W/\"5bf7a0a9-ca6\"",
	// 	  "User-Agent": "Go-http-client/1.1"
	// 	},
	// 	"origin": "172.17.0.1",
	// 	"url": "http://httpbin.org/get"
	//   }
  • example3: GET request with path parameters

This example demonstrates how to parse a cURL command for a GET request with custom headers and path parameters, set the path parameter value, and execute the request.

	c := gcurl.Parse(`curl -X GET "http://httpbin.org/anything/1" -H "accept: application/json"`)
	tp := c.Temporary()
	pp := tp.PathParam(`anything/(\d+)`)
	pp.IntSet(100) // Set Param. 
	resp, err := tp.Execute()
	if err != nil {
		t.Error(err)
	}
	log.Println(string(resp.Content()))
	// {
	//   "args": {}, 
	//   "data": "", 
	//   "files": {}, 
	//   "form": {}, 
	//   "headers": {
	//     "Accept": "application/json", 
	//     "Connection": "close", 
	//     "Host": "httpbin.org", 
	//     "User-Agent": "Go-http-client/1.1"
	//   }, 
	//   "json": null, 
	//   "method": "GET", 
	//   "origin": "172.17.0.1", 
	//   "url": "http://httpbin.org/anything/100"
	// }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(curlbash string) (*requests.Response, error)

Execute 直接执行curlbash

func GetRawCookies

func GetRawCookies(soptions string, filter string) []*http.Cookie

GetRawCookies parses all 'Cookie' values from the rawcookie and returns the successfully parsed Cookies.

if filter is not empty, only cookies of that name are returned

Types

type CURL

type CURL struct {
	ParsedURL *url.URL
	Method    string
	Header    http.Header
	CookieJar http.CookieJar
	Cookies   []*http.Cookie

	ContentType string
	Body        *bytes.Buffer

	Auth     *requests.BasicAuth
	Timeout  int // second
	Insecure bool
}

CURL 信息结构

func New

func New() *CURL

New new 一个 curl 出来

func Parse

func Parse(scurl string) (cURL *CURL)

Parse curl_bash

func (*CURL) CreateSession

func (curl *CURL) CreateSession() *requests.Session

CreateSession 创建Session

func (*CURL) CreateTemporary

func (curl *CURL) CreateTemporary(ses *requests.Session) *requests.Temporary

CreateTemporary 根据Session 创建Temporary

func (*CURL) String

func (curl *CURL) String() string

func (*CURL) Temporary

func (curl *CURL) Temporary() *requests.Temporary

Temporary 根据自己CreateSession 创建Temporary

Jump to

Keyboard shortcuts

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