A simple and useful HTTP client library for Golang HTTPC

Source: Internet
Author: User

HTTPC Introduction

Httpc This is a client library that initiates an HTTP request.
Its features include: easy to use, easy to expand, support chain call, support multi-format processing and so on.
Ideal for calling Restfull-style interfaces.

Project Address

Https://coding.net/u/recallsong/p/httpc/git

Download
go get git.coding.net/recallsong/httpc.git
API documentation

View API Documentation

We can also use the Godoc tool to view the API documentation locally:

godoc -http=:9090

To view the address in the browser:
Http://localhost:9090/pkg/coding.net/recallsong/httpc

The easiest way to get started with quick start
var resp string// GET http://localhost/hello?name=RecallSongerr := httpc.New("http://localhost").Path("hello").Query("name", "RecallSong").Get(&resp)if err != nil {    fmt.Println(resp) // 以字符串方式获取响应的数据} else {    fmt.Println(err)}
Set up request headers and cookies, etc.
var resp stringerr := httpc.New("http://localhost").Path("/hello").Query("param", "value").    Header("MyHeader", "HeaderValue").    AddCookie(&http.Cookie{Name: "cookieName", Value: "cookieValue"}).    Body("body data").Post(&resp)if err != nil {    fmt.Println(resp) // 以字符串方式获取响应的数据} else {    fmt.Println(err)}
Send and receive data in JSON format using map to pass data
body := map[string]interface{}{    "name": "RecallSong",    "age":  18,}var resp map[string]interface{}// 根据请求的Content-Type自动对数据进行转换err := httpc.New("http://localhost").Path("json").    ContentType(httpc.TypeApplicationJson).    Body(body). // body转变为 {"name":"RecallSong","age":18}    Post(&resp) // 根据响应中的Content-Type,将返回的数据解析到resp中fmt.Println(err, resp)// 如果请求或响应没有指定Content-Type,或是不正确,也可以强制指定转换格式类型err = httpc.New("http://localhost").Path("json").    Body(body, httpc.TypeApplicationJson). // body转变为 {"name":"RecallSong","age":18}    Post(&resp, httpc.TypeApplicationJson) // 将返回的数据按json格式解析到map中fmt.Println(err, resp)
Passing data using a struct
type Person struct {    Name string `json:"name"`    Age  int    `json:"age"`}body := Person{Name: "RecallSong", Age: 18}var resp Personerr := httpc.New("http://localhost").Path("json").    Body(body, httpc.TypeApplicationJson).    Post(&resp, httpc.TypeApplicationJson)fmt.Println(err, resp)
Send and receive data in XML format
type Person struct {    Name string `xml:"name"`    Age  int    `xml:"age"`}body := Person{Name: "RecallSong", Age: 18}var resp Personerr := httpc.New("http://localhost").Path("xml").    Body(body, httpc.TypeApplicationXml). // 数据转变为xml格式    Post(&resp, httpc.TypeApplicationXml)fmt.Println(err, resp)
Send form parameters using struct-body send
sbody := struct {    Name string `form:"name"`    Age  int    `form:"age"`}{    Name: "RecallSong",    Age:  18,}var resp stringerr := httpc.New("http://localhost").Path("echo").    Body(sbody, httpc.TypeApplicationForm). // 将结构体转变为form格式的数据体    Post(&resp)fmt.Println(err, resp)
Send using Map
mbody := map[string]interface{}{    "name": "RecallSong",    "age":  19,}var resp stringerr := httpc.New("http://localhost").Path("echo").    Body(mbody, httpc.TypeApplicationForm). // 将map变为form格式的数据体    Post(&resp)fmt.Println(err, resp)
Use a URL. Values Send
ubody := url.Values{}ubody.Set("name", "RecallSong")ubody.Set("age", "20")var resp stringerr := httpc.New("http://localhost").Path("echo").    Body(ubody). // 将url.Values类型转变form格式的数据体    Post(&resp)fmt.Println(err, resp)
Automatically encode URL path parameters
var resp string// 可以自动编码url路径参数err := httpc.New("http://localhost").EscapedPath("recall/Song").EscapedPath(18).Get(&resp)// 请求地址为 http://localhost/recall%2FSong/18fmt.Println(err, resp)
Upload file Mode 1
file, err := os.Open("doc.go")if err != nil {  fmt.Println(err)  return}defer file.Close()body := map[string]interface{}{    "file":  file,    "name":  "RecallSong",    "age":   18,    "file2": httpc.FilePath("doc.go:hello.go"), //上传doc.go文件,参数名为file2,文件名为hello.go}var resp stringerr = httpc.New("http://localhost").Path("echo").    Body(body, httpc.TypeMultipartFormData).Post(&resp)fmt.Println(err)
Mode 2
file, err := os.Open("doc.go")if err != nil {    fmt.Println(err)    return}defer file.Close()body := struct {    Name    string         `form:"name"`    Address []string       `form:"address"`    Age     int            `form:"age"`    File    *os.File       `form:"file" file:"hello.go"`    File2   httpc.FilePath `form:"file2"`}{    Name:    "RecallSong",    Address: []string{"HangZhou", "WenZhou"},    Age:     18,    File:    file,    File2:   httpc.FilePath("doc.go:hello2.go"), //上传doc.go文件,参数名为file2,文件名为hello2.go}var resp stringerr = httpc.New("http://localhost").Path("echo").    Body(body, httpc.TypeMultipartFormData).Post(&resp)fmt.Println(err)
Receive response data
// 前面的例子我们知道了可以接收json和xml格式的数据,也可以接收数据到一个string变量中// 除此之外,我们还可以有一下几种方式接收数据// []byte 方式接收var bytesResp []byteerr := httpc.New("http://localhost").Path("hello").Get(&bytesResp)fmt.Println(err, bytesResp)// *http.Response 方式接收var resp *http.Responseerr := httpc.New("http://localhost").Path("hello").Get(&resp)if err != nil {  fmt.Println(err)} else {  // 注意这种方式要关闭Body  defer resp.Body.Close()  body, err := ioutil.ReadAll(resp.Body)  fmt.Println(err, string(body))}
Download file Mode 1
// 默认方式保存文件err := httpc.New("http://localhost").Path("echo").Body("content").Post(httpc.FilePath("download1.txt"))fmt.Println(err)
Mode 2
err := httpc.New("http://localhost").Path("echo").Body("content").Post(&httpc.SaveInfo{    Path:     "download2.txt",    Override: true,    Mode:     0777})fmt.Println(err)
Specify a successful HTTP status code
// 如果返回的状态码与指定的状态码不匹配,则返回一个errorerr := httpc.New("http://localhost").Path("not_exist").    SuccessStatus(200).Get(nil)fmt.Println(err)// Output:// error http status 404 , expect 200
Request context
// 请求上下文中包含了每次请求的设置、连接设置等,所有请求应该尽量共享Context// 我们可以设置回调通知的函数ctx := httpc.NewContext().AddCbBeforeSend(func(client *httpc.HttpC, args ...interface{}) error {    fmt.Println("before request")    return nil}).AddCbAfterSend(func(client *httpc.HttpC, args ...interface{}) error {    fmt.Println("after response")    return nil}).AddCbOnError(func(client *httpc.HttpC, args ...interface{}) error {    fmt.Println("on error")    return nil}).SetConnectReadTimeout(30*time.Second, 30*time.Second)var resp stringerr := httpc.New("http://localhost").Path("hello").SetContext(ctx).Get(&resp)fmt.Println(err, resp)// 库默认生成了一个上下文实例 httpc.DefaultContext,它并没有加锁保护,所以尽量在所有请求前设置好它// 改变httpc.DefaultContext会影响所有未调用过SetContext的请求httpc.DefaultContext.SetConnectReadTimeout(30*time.Second, 30*time.Second)err = httpc.New("http://localhost").Path("hello").Get(&resp)fmt.Println(err, resp)
Timeout settings
err := httpc.New("http://localhost").Path("timeout").SetContext(httpc.NewContext().SetConnectReadTimeout(time.Second, time.Second)).  Get(nil)fmt.Println(err)
Request Retry
err := httpc.New("http://not_exist/").Path("not_exist").    SetContext(httpc.NewContext().AddCbOnRetring(func(c *httpc.HttpC, args ...interface{}) error {        fmt.Printf("retring %v, next interval %v\n", args[0], args[1])        return nil    }).SetRetryConfig(3, time.Second, 2)). // 重试3次,重试时间间隔依次为:2s, 4s, 8s    Get(nil)fmt.Println(err)// Output:// retring 1, next interval 2s// retring 2, next interval 4s// retring 3, next interval 8s// Get http://not_exist/not_exist: dial tcp: lookup not_exist: no such host
Custom Request or response processor
// httpc库已经注册了一些通用的请求和响应处理器,但我们也可以额外添加处理器ctx := httpc.NewContext()ctx.BodyReaders = httpc.NewBodyReaders()ctx.BodyReaders.RespBodyTypeReaders[reflect.TypeOf((*int)(nil))] = func(resp *http.Response, reader io.ReadCloser, typ reflect.Type, out interface{}) error {    output := out.(*int)    *output = resp.StatusCode    return nil}// 返回响应状态码var status interr := httpc.New("http://localhost").Path("hello").    SetContext(ctx).    Get(&status)fmt.Println(err, status)// Output:// <nil> 200
Other features

Please refer to API documentation or source code

A simple and useful HTTP client library for Golang HTTPC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.