Skip to content

Instantly share code, notes, and snippets.

@diyan
Last active October 14, 2019 08:11
Show Gist options
  • Save diyan/ddf637d3be0bd191bc447525f97087f3 to your computer and use it in GitHub Desktop.
Save diyan/ddf637d3be0bd191bc447525f97087f3 to your computer and use it in GitHub Desktop.
Go HTTP clients. Sample code in two common use-cases

Go HTTP clients. Sample code in common use-cases

  • Use case #1: Work with JSON using strings, make a HTTP POST
  • Use case #2: Work with JSON using structs, make a HTTP POST

Describe how to change various HTTP request attributes that usually differs from one request to another:

  • HTTP headers
  • Request timeout
  • TODO query string parameters

Do not describe HTTP request/client/transport attributes that usually common for a set of requests:

  • Authentication
  • HTTP compression
  • HTTP proxy
  • HTTP redirect policy
  • TLS configuration
  • Rest of the http.Transport configuration
JSON as strings using stdlib, 3x err
client := &http.Client{
    Timeout: 60 * time.Second,
}
reqBody := `{...}`
req, err := http.NewRequest("POST", "http://example.com/json", strings.NewReader(reqBody))
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
bytes, err := ioutil.ReadAll(res.Body)
result := string(bytes)
JSON as structs using stdlib, 5x err
client := &http.Client{
    Timeout: 60 * time.Second,
}
reqBody := ReqStruct{}
reqBytes, err := json.Marshal(reqBody)
req, err := http.NewRequest("POST", "http://example.com/json", bytes.NewBuffer(reqBytes))
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
bytes, err := ioutil.ReadAll(res.Body)
okStruct := OkStruct{}
err = json.Unmarshal(bytes, &okStruct)
JSON as strings using parnurzeal/gorequest, 1x errs
reqBody := `{...}`
res, bodyStr, errs := gorequest.New().Timeout(60 * time.Second).
    Set("Accept", "application/json").
    Post("https://example.com/json").
    SendString(reqBody).
    End()
JSON as structs using parnurzeal/gorequest, 1x errs
reqBody := ReqStruct{}
okStruct := OkStruct{}
res, _, errs := gorequest.New().Timeout(60 * time.Second).
    Set("Accept", "application/json").
    Post("https://example.com/json").
    SendStruct(reqBody).
    EndStruct(&okStruct)
JSON as strings using levigross/grequests, 1x err
reqBody := `{...}`
ro := &RequestOptions{
    RequestBody: strings.NewReader(reqBody),
    Headers: map[string]string{"Accept": "application/json"},
    RequestTimeout: 60 * time.Second,
}
res, err := grequests.Post("https://example.com/json", ro)
result := res.String()
JSON as structs using levigross/grequests, 2x err
reqBody := ReqStruct{}
ro := &RequestOptions{
    JSON: reqBody,
    Headers: map[string]string{"Accept": "application/json"},
    RequestTimeout: 60 * time.Second,
}
res, err := grequests.Post("https://example.com/json", ro)
okStruct := OkStruct{}
err = res.JSON(&okStruct)
JSON as strings using dghubble/sling, 2x err
reqBody := `{...}`
client := &http.Client{
    Timeout: 60 * time.Second,
}
res, err := sling.New().Client(client).
    Set("Accept", "application/json").
    Post("https://example.com/json").
    Body(strings.NewReader(reqBody)).
    Receive(nil, nil)
bytes, err := ioutil.ReadAll(res.Body)
result := string(bytes)
// NOTE Implement ReceiveString(successV, failureV *string) (*http.Response, error) ?
JSON as structs using dghubble/sling, 1x err
reqBody := ReqStruct{}
client := &http.Client{
    Timeout: 60 * time.Second,
}
okStruct := OkStruct{}
errStruct := ErrStruct{}
res, err := sling.New().Client(client).
    Set("Accept", "application/json").
    Post("https://example.com/json").
    BodyJSON(reqBody).
    Receive(&okStruct, &errStruct)
JSON as strings using go-resty/resty, 1x err
reqBody := `{...}`
res, err := resty.R().SetTimeout(60 * time.Second).
    SetHeader("Accept", "application/json").
    SetBody(reqBody).
    Post("https://example.com/json")
result := res.String()
JSON as structs using go-resty/resty, 1x err
reqBody := ReqStruct{}
res, err := resty.R().SetTimeout(60 * time.Second).
    SetHeader("Accept", "application/json").
    SetBody(reqBody).
    SetResult(OkStruct{}).SetError(ErrStruct{}).
    Post("https://example.com/json")
okStruct := response.Result().(*OkStruct)
errStruct := response.Error().(*ErrStruct)
JSON as strings using h2non/gentleman, 1x err
reqBody := `{...}`
res, err := gentleman.New().Request().
    URL("http://example.com/json").
    Method("POST").
    Use(timeout.Request(60 * time.Second)).
    Use(headers.Set("Accept", "application/json")).
    Use(body.JSON(reqBody)).
    Send()
result := res.String()
JSON as structs using h2non/gentleman, 2x err
reqBody := ReqStruct{}
res, err := gentleman.New().Request().
    URL("http://example.com/json").
    Method("POST").
    Use(timeout.Request(60 * time.Second)).
    Use(headers.Set("Accept", "application/json")).
    Use(body.JSON(reqBody)).
    Send()
okStruct := OkStruct{}
err := res.JSON(&okStruct)
@insomnius
Copy link

Nice one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment