From 5fc859eabd5c2e38b10cbb63f057e83da0748831 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Tue, 19 Jun 2018 17:48:04 +0200 Subject: Set Content-Type to json in httpclient lib Change-Id: I1f1dcb613c685aeac9981d85460b7899bb323a4d Signed-off-by: Sebastien Douheret --- golib/httpclient.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/golib/httpclient.go b/golib/httpclient.go index 6662b2d..f4880e6 100644 --- a/golib/httpclient.go +++ b/golib/httpclient.go @@ -33,6 +33,7 @@ type HTTPClient struct { // HTTPClientConfig is used to config HTTPClient type HTTPClientConfig struct { URLPrefix string + ContentType string HeaderAPIKeyName string Apikey string HeaderClientKeyName string @@ -87,6 +88,11 @@ func HTTPNewClient(baseURL string, cfg HTTPClientConfig) (*HTTPClient, error) { */ } + // Default set Content-Type to json + if client.conf.ContentType == "" { + client.conf.ContentType = "application/json" + } + if err := client.getCidAndCsrf(); err != nil { client.log(HTTPLogLevelError, "Cannot retrieve Client ID and/or CSRF: %v", err) return &client, err @@ -293,6 +299,9 @@ func (c *HTTPClient) _HTTPRequest(method, url string, body *string, data *[]byte } func (c *HTTPClient) handleRequest(request *http.Request) (*http.Response, error) { + if c.conf.ContentType != "" { + request.Header.Set("Content-Type", c.conf.ContentType) + } if c.conf.HeaderAPIKeyName != "" && c.apikey != "" { request.Header.Set(c.conf.HeaderAPIKeyName, c.apikey) } -- cgit 1.2.3-korg