/go-jsonclient

Http client to handle json request and response written in go

Primary LanguageGoMIT LicenseMIT

Go Json Client

Build Status Go Report Card GoDoc

Go Json Client simplify the http request in json.

It uses net/http core package as http client.

Install

This library require golang at version >= 1.13

go get -u github.com/davidebianchi/go-jsonclient

Example usage

Make a request

If you want to create a json client to call a specific BaseUrl with default authentication headers:

func handleRequest () {
  opts := Options{
    BasePath: apiURL,
    Headers: Headers{
      "some":  "header",
      "other": "value",
    },
  }
  client := jsonclient.New(opts)

  var data = map[string]interface{}{
    "some": "json format",
    "foo":  "bar",
    "that": float64(3),
  }
  req, err := client.NewRequest(http.MethodPost, "my-resource", data)
  if err != nil {
    panic("Error creating request")
  }

  type Response struct {
    my string
  }
  v := Response{}
  // server response is: {"my": "data"}
  response, err := client.Do(req, &v)
  if err != nil {
    panic("Error making request")
  }

  if Response.my != "data" {
    panic("response data is not mine")
  }
}

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.