• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

pcurl是解析curl命令的库,弥补go生态链的一块空白[从零实现]

pcurl

Go codecov

pcurl是解析curl表达式的库

feature

  • 支持-X; --request,作用设置GET或POST的选项
  • 支持-H; --header选项,curl中用于设置http header的选项
  • 支持-d; --data选项,作用设置http body
  • 支持--data-raw选项,curl用于设置http body
  • 支持-F --form选项,用作设置formdata
  • 支持--url选项,curl中设置url,一般不会设置这个选项
  • 支持--compressed选项
  • 支持-k, --insecure选项
  • 支持-G, --get选项
  • 支持-i, --include选项
  • 支持--data-urlencode选项
  • 支持内嵌到你的结构体里面,让你的cmd秒变curl

内容

quick start

package main

import (
    "fmt"
    "github.com/antlabs/pcurl"
    //"github.com/guonaihong/gout"
    "io"
    "io/ioutil"
    "net/http"
)

func main() {
    req, err := pcurl.ParseAndRequest(`curl -X POST -d 'hello world' www.qq.com`)
    if err != nil {
        fmt.Printf("err:%s\n", err)
        return
    }

    resp, err := http.DefaultClient.Do(req)
    n, err := io.Copy(ioutil.Discard, resp.Body)
    fmt.Println(err, "resp.size = ", n)

    /*
        resp := ""
        err = gout.New().SetRequest(req).BindBody(&resp).Do()

        fmt.Println(err, "resp.size = ", len(resp))
    */
}

json

package main

import (
    "fmt"
    "github.com/antlabs/pcurl"
    "io"
    "net/http"
    "os"
)

func main() {
    req, err := pcurl.ParseAndRequest(`curl -XPOST -d '{"hello":"world"}' 127.0.0.1:1234`)
    if err != nil {
        fmt.Printf("err:%s\n", err)
        return
    }   

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        fmt.Printf("err:%s\n", err)
        return
    }   
    defer resp.Body.Close()

    io.Copy(os.Stdout, resp.Body)
}

form data

package main

import (
    "fmt"
    "github.com/antlabs/pcurl"
    "io"
    "net/http"
    "os"
)

func main() {
    req, err := pcurl.ParseAndRequest(`curl -XPOST -F mode=A -F text='Good morning' 127.0.0.1:1234`)
    if err != nil {
        fmt.Printf("err:%s\n", err)
        return
    }   

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        fmt.Printf("err:%s\n", err)
        return
    }   
    defer resp.Body.Close()

    io.Copy(os.Stdout, resp.Body)
}

dump to json

package main

import (
    "fmt"
    "github.com/antlabs/pcurl"
    "io"
    "net/http"
    "os"
)

func main() {
    all, err := pcurl.ParseAndJSON(`curl https://api.openai.com/v1/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer YOUR_API_KEY' -d '{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'`)
	fmt.Printf("%s\n", all)
/*
{
  "url": "https://api.openai.com/v1/completions",
  "encode": {
    "body": "json"
  },
  "body": {
    "max_tokens": 7,
    "model": "text-davinci-003",
    "prompt": "Say this is a test",
    "temperature": 0
  },
  "header": [
    "Content-Type: application/json",
    "Authorization: Bearer YOUR_API_KEY"
  ]
}
}
*/

dump struct

package main

import (
    "fmt"
    "github.com/antlabs/pcurl"
    "io"
    "net/http"
    "os"
)

func main() {
    all, err := pcurl.ParseAndObj(`curl https://api.openai.com/v1/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer YOUR_API_KEY' -d '{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'`)

	fmt.Printf("%s\n", all)
/*
&pcurl.Req{Method:"POST", URL:"https://api.openai.com/v1/completions", Encode:pcurl.Encode{Body:"json"}, Body:map[string]interface {}{"max_tokens":7, "model":"text-davinci-003", "prompt":"Say this is a test", "temperature":0}, Header:[]string{"Content-Type: application/json", "Authorization: Bearer YOUR_API_KEY"}}
*/

继承pcurl的选项(curl)--让你的cmd秒变curl

自定义的Gen命令继续pcurl所有特性,在此基础加些自定义选项。

type Gen struct {
    //curl选项
	pcurl.Curl

    //自定义选项
	Connections string        `clop:"-c; --connections" usage:"Connections to keep open"`
	Duration    time.Duration `clop:"--duration" usage:"Duration of test"`
	Thread      int           `clop:"-t; --threads" usage:"Number of threads to use"`
	Latency     string        `clop:"--latency" usage:"Print latency statistics"`
	Timeout     time.Duration `clop:"--timeout" usage:"Socket/request timeout"`
}

func main() {
	g := &Gen{}

	clop.Bind(&g)

    // pcurl包里面提供
	req, err := g.SetClopAndRequest(clop.CommandLine)
	if err != nil {
		panic(err.Error())
	}

    // 已经拿到http.Request对象
    // 如果是标准库直接通过Do()方法发送
    // 如果是裸socket,可以通过http.DumpRequestOut先转成[]byte再发送到服务端
    fmt.Printf("%p\n", req)
}

More Repositories

1

strsim

Calculate string similarity library, integrate multiple algorithms on the back end。计算字符串相似度库,后端集成多种算法[从零实现]
Go
273
star
2

timer

High-performance timer implementation based on 5-level time wheel. 高性能定时器(5级时间轮,最小堆)[从零实现]
Go
263
star
3

quickws

高性能websocket库, Callback写法,在高频cpu上有不俗表现 https://github.com/antlabs/quickws-example
Go
96
star
4

pcopy

pcopy是深度拷贝库,相比上个版本(v0.0.10),性能提升4-10倍
Go
89
star
5

greatws

100w连接仅需500-700MB内存,针对海量连接特别优化的websocket库(kqueue, epoll),高性能,callback写法,在服务器cpu上有不俗表现 https://github.com/antlabs/greatws-example
Go
80
star
6

httparser

高性能http 1.1解析器,为你的异步io库插上http解析的翅膀, 每秒可以处理630.15MB/s流量[从零实现]
Go
41
star
7

gstl

快写完了....支持泛型的数据结构库(vec, linkedlist, skiplist, hashtable, btree, avltree, rbtree, trie, set
Go
26
star
8

tostruct

Generate struct definition according to json/yaml/query string/http header string @^^@ 根据json/yaml/query string/http header字符串生成struct[从零实现]
Go
16
star
9

cronex

高性能cron库,crontab语法默认支持到秒级
Go
9
star
10

mock

生成mock数据
Go
7
star
11

gout-middleware

gout中间件
Go
6
star
12

brouter

高性能http router库,API风格类似httprouter,比1.3.0的httprouter快50-60%的样子,比开发版本的httprouter慢一点,大约是 92-95%的性能。[从零实现]
Go
5
star
13

h2o

脚手架工具,统一的dsl,方便生成一些代码(1. 配置生成http client/server代码 2. 从json/yaml生成结构体定义 3. 生成grpc protobuf)
Go
5
star
14

cat

golang实现的cat命令(所有功能),也可以看成https://github.com/guonaihong/clop 的使用示例
Go
2
star
15

greatws-example

greatws的example
Go
1
star
16

stl

纯c风格实现的高性能数据结构
Go
1
star
17

deepcopy

存放原deepcoppy v0.0.10版本的代码
Go
1
star
18

wsutil

websocket的工具函数
Go
1
star
19

quickws-example

quickws的example代码
Go
1
star
20

easychan

对于chan使用场景,总结与收集好用的工具函数
Go
1
star