Skip to content

prongbang/callx

Repository files navigation

CallX

CallX HTTP Client easy call API for Golang

Build Status Codecov Go Report Card

"Buy Me A Coffee"

Install

go get github.com/prongbang/callx

Benchmark

Benchmark_CallXRequests/GET-10         	   41756	     31823 ns/op
Benchmark_CallXRequests/POST-10        	   38692	     35787 ns/op
Benchmark_CallXRequests/POST-ENCODE-10 	   28848	     39314 ns/op
Benchmark_CallXRequests/PUT-10         	   31401	     35046 ns/op
Benchmark_CallXRequests/PATCH-10       	   38923	     30094 ns/op
Benchmark_CallXRequests/DELETE-10      	   41100	     29195 ns/op

How to use

  • Using base URL
c := callx.Config{
    BaseURL: "https://jsonplaceholder.typicode.com",
    Timeout: 60,
}
req := callx.New(c)

data := req.Get("/todos/1")
fmt.Println(string(data.Data))
  • Custom request
c := callx.Config{
    Timeout: 60,
}
req := callx.New(c)

custom := callx.Custom{
    URL:    "https://httpbin.org/post",
    Method: http.MethodPost,
    Header: callx.Header{
        callx.Authorization: fmt.Sprintf("%s %s", callx.Bearer, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.Et9HFtf9R3GEMA0IICOfFMVXY7kkTX1wr4qCyhIf58U"),
    },
    Body: callx.Body{
        "username": "root",
        "password": "pass",
        "address": []string{
            "087654321",
            "089786756",
        },
    },
}
data := req.Req(custom)
fmt.Println(string(data.Data))
  • Custom request form encoded
c := callx.Config{
    Timeout: 60,
}
req := callx.New(c)

form := url.Values{}
form.Set("message", "Test")

custom := callx.Custom{
    URL:    "https://httpbin.org/post",
    Method: http.MethodPost,
    Header: callx.Header{
        callx.Authorization: "Bearer XTZ",
        callx.ContentType:   "application/x-www-form-urlencoded",
    },
    Form: strings.NewReader(form.Encode()),
}
data := req.Req(custom)
fmt.Println(string(data.Data))