Skip to content
Snippets Groups Projects
Commit 8be90307 authored by ale's avatar ale
Browse files

Pass a Context do DoJSONHTTPRequest

parent 41657e75
No related branches found
No related tags found
No related merge requests found
...@@ -2,13 +2,17 @@ package clientutil ...@@ -2,13 +2,17 @@ package clientutil
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
) )
func DoJSONHTTPRequest(client *http.Client, uri string, req, resp interface{}) error { // DoJSONHTTPRequest makes an HTTP POST request to the specified uri,
// with a JSON-encoded request body. It will attempt to decode the
// response body as JSON.
func DoJSONHTTPRequest(ctx context.Context, client *http.Client, uri string, req, resp interface{}) error {
data, err := json.Marshal(req) data, err := json.Marshal(req)
if err != nil { if err != nil {
return err return err
...@@ -19,6 +23,7 @@ func DoJSONHTTPRequest(client *http.Client, uri string, req, resp interface{}) e ...@@ -19,6 +23,7 @@ func DoJSONHTTPRequest(client *http.Client, uri string, req, resp interface{}) e
return err return err
} }
httpReq.Header.Set("Content-Type", "application/json") httpReq.Header.Set("Content-Type", "application/json")
httpReq = httpReq.WithContext(ctx)
httpResp, err := RetryHTTPDo(client, httpReq, NewExponentialBackOff()) httpResp, err := RetryHTTPDo(client, httpReq, NewExponentialBackOff())
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment