Last active
September 1, 2019 17:58
-
-
Save alessiosavi/d99e3727144dec7972f34143924406e8 to your computer and use it in GitHub Desktop.
Execute POST GET request in golang, create headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package request | |
import ( | |
"bytes" | |
"encoding/json" | |
"go.uber.org/zap" | |
"io/ioutil" | |
"net/http" | |
"strings" | |
) | |
// CreateHeaderList is delegated to initialize a list of headers. | |
// Every row of the matrix contains [key,value] | |
func CreateHeaderList(headers ...string) [][]string { | |
var list [][]string | |
lenght := len(headers) | |
list = make([][]string, lenght/2) | |
counter := 0 | |
for i := 0; i < lenght; i += 2 { | |
tmp := make([]string, 2) | |
key := headers[i] | |
value := headers[i+1] | |
tmp[0] = key | |
tmp[1] = value | |
zap.S().Debug("createHeaderList | ", i, ") Key: ", key, " Value: ", value) | |
list[counter] = tmp | |
counter++ | |
} | |
zap.S().Debug("createHeaderList | LIST: ", list) | |
return list | |
} | |
func SendRequest(url, method string, headers [][]string, jsonStr []byte) map[string]string { | |
// Create a custom request | |
var req *http.Request | |
var err error | |
// Used for decode the JSON response | |
var result map[string]string | |
switch method { | |
case "GET": | |
zap.S().Debug("sendRequest | Creating GET REQ") | |
req, err = http.NewRequest("GET", url, nil) | |
case "POST": | |
zap.S().Debug("sendRequest | Creating GET POST") | |
req, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | |
case "PUT": | |
zap.S().Debug("sendRequest | Creating GET PUT") | |
return nil | |
default: | |
zap.S().Debug("sendRequest | Unkown method -> ", method) | |
return nil | |
} | |
if err != nil { | |
zap.S().Error("AuthenticateRequest | Unable to create request! ", err) | |
return nil | |
} | |
lenght := len(headers) | |
for i := 0; i < lenght; i++ { | |
zap.S().Debug("sendRequest | Adding header: ", headers[i], " Len: ", len(headers[i])) | |
key := headers[i][0] | |
value := headers[i][1] | |
if strings.Compare(`Authorization`, key) == 0 { | |
req.Header.Add(key, value) | |
} else { | |
req.Header.Set(key, value) | |
} | |
zap.S().Debug("sendRequest | Adding header: {", key, "|", value, "}") | |
} | |
zap.S().Debug("sendRequest | Executing request ...") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
zap.S().Debug("Error on response.\n[ERRO] -", err) | |
return nil | |
} | |
defer resp.Body.Close() | |
zap.S().Debug("sendRequest | Request executed, reading response ...") | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
zap.S().Error("sendRequest | Unable to read response! ", err) | |
return nil | |
} | |
json.Unmarshal(body, &result) | |
return result | |
} | |
/* | |
Usage | |
func PingUrl(token, url string) bool { | |
url += `/all_dbs` | |
headers := request.CreateHeaderList("Authorization", "Bearer "+token) | |
fmt.Println(request.SendRequest(url, `GET`, headers, nil)) | |
return true | |
} | |
func RetrieveToken(apikey string) string { | |
headers := request.CreateHeaderList(`Accept`, `application/json`, `Content-Type`, `application/x-www-form-urlencoded`) | |
encoded := url.Values{} | |
encoded.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey") | |
encoded.Set("apikey", apikey) | |
url := "https://iam.cloud.ibm.com/identity/token" | |
token := request.SendRequest(url, `POST`, headers, []byte(encoded.Encode())) | |
zap.S().Debug("Token retrieved -> ", token) | |
return token[`access_token`] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment