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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto