POST with NewRequest using Golang
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
) | |
const ( | |
clientSecret string = "9ca19353e1fb861f6d3aed8af6803c0b" | |
accessToken string = "d42cd44868d2fe953329677aa1c71e1d80f0f4d7" | |
siteHost string = "http://coop.apps.knpuniversity.com" | |
userID string = "460" | |
) | |
func main() { | |
client := &http.Client{} | |
data := url.Values{} | |
data.Set("client_id", `Lazy Test`) | |
data.Add("client_secret", clientSecret) | |
data.Add("grant_type", "client_credentials") | |
req, err := http.NewRequest("POST", fmt.Sprintf("%s/token", siteHost), bytes.NewBufferString(data.Encode())) | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") // This makes it work | |
if err != nil { | |
log.Println(err) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Println(err) | |
} | |
f, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
resp.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(f)) | |
} |
This comment has been minimized.
This comment has been minimized.
artinu777
commented
Feb 25, 2019
Thank you |
This comment has been minimized.
This comment has been minimized.
deepakjacob
commented
Jul 24, 2019
•
Yes, without Content-Type |
This comment has been minimized.
This comment has been minimized.
jdr430
commented
Aug 15, 2019
Thank you!! |
This comment has been minimized.
This comment has been minimized.
KanybekMomukeyev
commented
Dec 4, 2019
Very helpfull! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
temoto commentedJan 12, 2019
Thank you.👍