Giter Club home page Giter Club logo

gcurl's Introduction

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
  • Supports copying as cURL command only (Bash,Cmd)

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"
	// }

gcurl's People

Contributors

474420502 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gcurl's Issues

request is not parsed correctly

I'm finding issues with your libraries, a lot of time parsing doesn't work and you should return an error which is usually done in go, please do this, it's a good practice to have

can't parse curl without "http://" prefix

Users can use curl without "http://" prefix, and Linux curl will insert this prefix automatically, such as

curl baidu.com

and gcurl lib will panic as follows

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x5b5d0c]

=== RUN   TestLoadCurlStepsFromFile
--- FAIL: TestLoadCurlStepsFromFile (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x5b5d0c]

github.com/474420502/gcurl.ParseBash({0x700818, 0xe})
        /home/somebody/go/pkg/mod/github.com/474420502/[email protected]/parse_curl.go:318 +0x3ec

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.