I have the hostname
and a json
formated string. I want to insert the hostname
inside the value string of a key in that json
formated string.
My full code:
func pager() string {
token := "xxxxxxxxxxx"
url := "https://api.pagerduty.com/incidents"
hostname, err := os.Hostname()
fmt.Println(hostname, err)
jsonStr := []byte(`{
"incident": {
"type": "incident",
**"title": "Docker is down on."+hostname,**
"service": {
"id": "PWIXJZS",
"type": "service_reference"
},
"priority": {
"id": "P53ZZH5",
"type": "priority_reference"
},
"urgency": "high",
"incident_key": "baf7cf21b1da41b4b0221008339ff357",
"body": {
"type": "incident_body",
"details": "A disk is getting full on this machine. You should investigate what is causing the disk to fill, and ensure that there is an automated process in place for ensuring data is rotated (eg. logs should have logrotate around them). If data is expected to stay on this disk forever, you should start planning to scale up to a larger disk."
},
"escalation_policy": {
"id": "PT20YPA",
"type": "escalation_policy_reference"
}
}
}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/vnd.pagerduty+json;version=2")
req.Header.Set("From", "shinoda@wathever.com")
req.Header.Set("Authorization", "Token token="+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp.Status
}
func main() {
fmt.Println(pager())
}
I am not very familiar with go, in python I can do this easily and I dont know the proper way to do this in golang.
If someone could explain me, I would be grateful.
thanks in advance.