3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
24 "main/components/kafkacollector"
27 log "github.com/sirupsen/logrus"
30 var httpclient = &http.Client{}
32 // Send a http request with json (json may be nil)
33 func SendHttpRequest(json []byte, method string, url string, retry bool, useAuth bool) bool {
35 // set the HTTP method, url, and request body
39 req, err = http.NewRequest(method, url, http.NoBody)
41 req, err = http.NewRequest(method, url, bytes.NewBuffer(json))
42 req.Header.Set("Content-Type", "application/json; charset=utf-8")
45 log.Error("Cannot create http request, method: ", method, " url: ", url)
50 token, err := kafkacollector.FetchToken()
52 log.Error("Cannot fetch token for http request: ", err)
55 req.Header.Set("Authorization", "Bearer "+token.TokenValue)
58 log.Debug("HTTP request: ", req)
60 log.Debug("Sending http request")
61 resp, err2 := httpclient.Do(req)
63 log.Error("Http request error: ", err2)
64 log.Error("Cannot send http request method: ", method, " url: ", url)
66 if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 204 {
67 log.Debug("Accepted http status: ", resp.StatusCode)
71 log.Debug("HTTP resp: ", resp)