func (snClient *ServiceNowClient) doRequest(req *http.Request) ([]byte, error) {
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", snClient.authHeader)
	resp, err := snClient.client.Do(req)
	if err != nil {
		log.Errorf("Error sending the request. %s", err)
		return nil, err
	}
	serviceNowRequests.WithLabelValues(req.URL.Host, req.Method, strconv.Itoa(resp.StatusCode)).Inc()
	serviceNowLastRequest.SetToCurrentTime()
	if resp.StatusCode >= 400 {
		errorMsg := fmt.Sprintf("ServiceNow returned the HTTP error code: %v", resp.StatusCode)
		log.Error(errorMsg)
		return nil, errors.New(errorMsg)
	}
	defer resp.Body.Close()
	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Errorf("Error reading the body. %s", err)
		return nil, err
	}
	if !json.Valid(responseBody) {
		if strings.Contains(string(responseBody), hibernatingInstance) {
			return nil, errors.New("ServiceNow is in sleeping mode and is unavailable (Hibernating Instance)")
		}
		return nil, errors.New("ServiceNow is unavailable (API return format is not valid JSON)")
	}
	return responseBody, nil
}
doRequest will do the given ServiceNow request and return response as byte array
package main
import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
	"github.com/prometheus/common/log"
)
const (
	serviceNowBaseURL   = "https://%s.service-now.com"
	tableAPI            = "%s/api/now/v2/table/%s"
	hibernatingInstance = "Hibernating Instance"
)
type Incident map[string]interface{}
func (i Incident) GetSysID() string {
	return i["sys_id"].(string)
}
func (i Incident) GetNumber() string {
	return i["number"].(string)
}
func (i Incident) GetState() json.Number {
	return json.Number(i["state"].(string))
}
type IncidentResponse map[string]interface{}
func (ir IncidentResponse) GetResult() Incident {
	var incident Incident = ir["result"].(map[string]interface{})
	return incident
}
type IncidentsResponse map[string]interface{}
func (ir IncidentsResponse) GetResults() []Incident {
	results := ir["result"].([]interface{})
	incidents := make([]Incident, len(results))
	for i, result := range results {
		incidents[i] = result.(map[string]interface{})
	}
	return incidents
}
type ServiceNow interface {
	CreateIncident(tableName string, incidentParam Incident) (Incident, error)
	GetIncidents(tableName string, params map[string]string) ([]Incident, error)
	UpdateIncident(tableName string, incidentParam Incident, sysID string) (Incident, error)
}
type ServiceNowClient struct {
	baseURL    string
	authHeader string
	client     *http.Client
}
func NewServiceNowClient(instanceName string, userName string, password string) (*ServiceNowClient, error) {
	if instanceName == "" {
		return nil, errors.New("Missing instanceName")
	}
	if userName == "" {
		return nil, errors.New("Missing userName")
	}
	if password == "" {
		return nil, errors.New("Missing password")
	}
	return &ServiceNowClient{
		baseURL:    fmt.Sprintf(serviceNowBaseURL, instanceName),
		authHeader: fmt.Sprintf("Basic %s", base64.URLEncoding.EncodeToString([]byte(userName+":"+password))),
		client:     http.DefaultClient,
	}, nil
}
func (snClient *ServiceNowClient) create(table string, body []byte) ([]byte, error) {
	url := fmt.Sprintf(tableAPI, snClient.baseURL, table)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
	if err != nil {
		log.Errorf("Error creating the request. %s", err)
		return nil, err
	}
	return snClient.doRequest(req)
}
func (snClient *ServiceNowClient) get(table string, params map[string]string) ([]byte, error) {
	url := fmt.Sprintf(tableAPI, snClient.baseURL, table)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Errorf("Error creating the request. %s", err)
		return nil, err
	}
	q := req.URL.Query()
	for key, val := range params {
		q.Add(key, val)
	}
	req.URL.RawQuery = q.Encode()
	return snClient.doRequest(req)
}
func (snClient *ServiceNowClient) update(table string, body []byte, sysID string) ([]byte, error) {
	url := fmt.Sprintf(tableAPI+"/%s", snClient.baseURL, table, sysID)
	req, err := http.NewRequest("PUT", url, bytes.NewBuffer(body))
	if err != nil {
		log.Errorf("Error creating the request. %s", err)
		return nil, err
	}
	return snClient.doRequest(req)
}