Golang POST json
up vote
-1
down vote
favorite
I am struggling to make a POST in golang, I came from python so I am a little bit lost.
this is my code:
func pager() string
token := "xxxxxxxxxxx"
url := "https://api.pagerduty.com/incidents"
jsonStr := byte(`
"incident":
"type": "incident",
"title": "The server is on fire.",
"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())
What am I doing wrong?
I can see the headers are being passed ok, but the body is not being passed as json and I don't know why.
Can someone help me to understand how to make a proper json POST
request in golang?
Thanks in advance.
json go post
add a comment |
up vote
-1
down vote
favorite
I am struggling to make a POST in golang, I came from python so I am a little bit lost.
this is my code:
func pager() string
token := "xxxxxxxxxxx"
url := "https://api.pagerduty.com/incidents"
jsonStr := byte(`
"incident":
"type": "incident",
"title": "The server is on fire.",
"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())
What am I doing wrong?
I can see the headers are being passed ok, but the body is not being passed as json and I don't know why.
Can someone help me to understand how to make a proper json POST
request in golang?
Thanks in advance.
json go post
At first glance the code looks it should work. What is the response body returned by the server?
– ThunderCat
Nov 11 at 0:47
@ThunderCat This is the response I get &POST api.pagerduty.com/incidents HTTP/1.1 1 1 map[Content-Type:[application/json] Accept:[application/vnd.pagerduty+json;version=2] From:[shinoda@whatever.com] Authorization:[Token token=xxxxxxxxxxx]] 0x11e02f0 817 false api.pagerduty.com map map <nil> map <nil> <nil> <nil> <nil> 400 Bad Request
– Dimitri Berveglieri
Nov 11 at 0:50
Some services include a helpful explanation about why the request was bad in the response body. What is the response body returned by the server? One easy way to view the body is to copy it to stdout:io.Copy(os.Stdout, resp.Body)
– ThunderCat
Nov 11 at 1:16
@ThunderCat, Thank you very much. After I got the body response with io.Copy(os.Stdout, resp.Body) I could realize the id I was passing in the incident was wrong. worked like a charm. thanks a lot for your help.
– Dimitri Berveglieri
Nov 11 at 1:39
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am struggling to make a POST in golang, I came from python so I am a little bit lost.
this is my code:
func pager() string
token := "xxxxxxxxxxx"
url := "https://api.pagerduty.com/incidents"
jsonStr := byte(`
"incident":
"type": "incident",
"title": "The server is on fire.",
"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())
What am I doing wrong?
I can see the headers are being passed ok, but the body is not being passed as json and I don't know why.
Can someone help me to understand how to make a proper json POST
request in golang?
Thanks in advance.
json go post
I am struggling to make a POST in golang, I came from python so I am a little bit lost.
this is my code:
func pager() string
token := "xxxxxxxxxxx"
url := "https://api.pagerduty.com/incidents"
jsonStr := byte(`
"incident":
"type": "incident",
"title": "The server is on fire.",
"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())
What am I doing wrong?
I can see the headers are being passed ok, but the body is not being passed as json and I don't know why.
Can someone help me to understand how to make a proper json POST
request in golang?
Thanks in advance.
json go post
json go post
edited Nov 11 at 13:37
Shudipta Sharma
818211
818211
asked Nov 11 at 0:43
Dimitri Berveglieri
64
64
At first glance the code looks it should work. What is the response body returned by the server?
– ThunderCat
Nov 11 at 0:47
@ThunderCat This is the response I get &POST api.pagerduty.com/incidents HTTP/1.1 1 1 map[Content-Type:[application/json] Accept:[application/vnd.pagerduty+json;version=2] From:[shinoda@whatever.com] Authorization:[Token token=xxxxxxxxxxx]] 0x11e02f0 817 false api.pagerduty.com map map <nil> map <nil> <nil> <nil> <nil> 400 Bad Request
– Dimitri Berveglieri
Nov 11 at 0:50
Some services include a helpful explanation about why the request was bad in the response body. What is the response body returned by the server? One easy way to view the body is to copy it to stdout:io.Copy(os.Stdout, resp.Body)
– ThunderCat
Nov 11 at 1:16
@ThunderCat, Thank you very much. After I got the body response with io.Copy(os.Stdout, resp.Body) I could realize the id I was passing in the incident was wrong. worked like a charm. thanks a lot for your help.
– Dimitri Berveglieri
Nov 11 at 1:39
add a comment |
At first glance the code looks it should work. What is the response body returned by the server?
– ThunderCat
Nov 11 at 0:47
@ThunderCat This is the response I get &POST api.pagerduty.com/incidents HTTP/1.1 1 1 map[Content-Type:[application/json] Accept:[application/vnd.pagerduty+json;version=2] From:[shinoda@whatever.com] Authorization:[Token token=xxxxxxxxxxx]] 0x11e02f0 817 false api.pagerduty.com map map <nil> map <nil> <nil> <nil> <nil> 400 Bad Request
– Dimitri Berveglieri
Nov 11 at 0:50
Some services include a helpful explanation about why the request was bad in the response body. What is the response body returned by the server? One easy way to view the body is to copy it to stdout:io.Copy(os.Stdout, resp.Body)
– ThunderCat
Nov 11 at 1:16
@ThunderCat, Thank you very much. After I got the body response with io.Copy(os.Stdout, resp.Body) I could realize the id I was passing in the incident was wrong. worked like a charm. thanks a lot for your help.
– Dimitri Berveglieri
Nov 11 at 1:39
At first glance the code looks it should work. What is the response body returned by the server?
– ThunderCat
Nov 11 at 0:47
At first glance the code looks it should work. What is the response body returned by the server?
– ThunderCat
Nov 11 at 0:47
@ThunderCat This is the response I get &POST api.pagerduty.com/incidents HTTP/1.1 1 1 map[Content-Type:[application/json] Accept:[application/vnd.pagerduty+json;version=2] From:[shinoda@whatever.com] Authorization:[Token token=xxxxxxxxxxx]] 0x11e02f0 817 false api.pagerduty.com map map <nil> map <nil> <nil> <nil> <nil> 400 Bad Request
– Dimitri Berveglieri
Nov 11 at 0:50
@ThunderCat This is the response I get &POST api.pagerduty.com/incidents HTTP/1.1 1 1 map[Content-Type:[application/json] Accept:[application/vnd.pagerduty+json;version=2] From:[shinoda@whatever.com] Authorization:[Token token=xxxxxxxxxxx]] 0x11e02f0 817 false api.pagerduty.com map map <nil> map <nil> <nil> <nil> <nil> 400 Bad Request
– Dimitri Berveglieri
Nov 11 at 0:50
Some services include a helpful explanation about why the request was bad in the response body. What is the response body returned by the server? One easy way to view the body is to copy it to stdout:
io.Copy(os.Stdout, resp.Body)
– ThunderCat
Nov 11 at 1:16
Some services include a helpful explanation about why the request was bad in the response body. What is the response body returned by the server? One easy way to view the body is to copy it to stdout:
io.Copy(os.Stdout, resp.Body)
– ThunderCat
Nov 11 at 1:16
@ThunderCat, Thank you very much. After I got the body response with io.Copy(os.Stdout, resp.Body) I could realize the id I was passing in the incident was wrong. worked like a charm. thanks a lot for your help.
– Dimitri Berveglieri
Nov 11 at 1:39
@ThunderCat, Thank you very much. After I got the body response with io.Copy(os.Stdout, resp.Body) I could realize the id I was passing in the incident was wrong. worked like a charm. thanks a lot for your help.
– Dimitri Berveglieri
Nov 11 at 1:39
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244834%2fgolang-post-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
At first glance the code looks it should work. What is the response body returned by the server?
– ThunderCat
Nov 11 at 0:47
@ThunderCat This is the response I get &POST api.pagerduty.com/incidents HTTP/1.1 1 1 map[Content-Type:[application/json] Accept:[application/vnd.pagerduty+json;version=2] From:[shinoda@whatever.com] Authorization:[Token token=xxxxxxxxxxx]] 0x11e02f0 817 false api.pagerduty.com map map <nil> map <nil> <nil> <nil> <nil> 400 Bad Request
– Dimitri Berveglieri
Nov 11 at 0:50
Some services include a helpful explanation about why the request was bad in the response body. What is the response body returned by the server? One easy way to view the body is to copy it to stdout:
io.Copy(os.Stdout, resp.Body)
– ThunderCat
Nov 11 at 1:16
@ThunderCat, Thank you very much. After I got the body response with io.Copy(os.Stdout, resp.Body) I could realize the id I was passing in the incident was wrong. worked like a charm. thanks a lot for your help.
– Dimitri Berveglieri
Nov 11 at 1:39