1 回答
TA贡献1807条经验 获得超9个赞
实施了@RedBlue 的建议:
package main
import (
"io/ioutil"
"log"
"net/http"
"strings"
"net/url"
)
type Payload struct {
Username string `json:"username"`
Password string `json:"password"`
GrantType string `json:"grant_type"`
Scope string `json:"scope"`
}
func main() {
const endpoint string = "https://api.io/v1/oauth/token"
formData := &Payload{
Username: "email",
Password: "pass",
GrantType: "password",
Scope: "SPACE SEPARATED STRINGS",
}
payload := url.Values{
"username": {formData.Username},
"password": {formData.Password},
"grant_type": {formData.GrantType},
"scope": {formData.Scope},
}
req, err := http.NewRequest("POST", endpoint, strings.NewReader(payload.Encode()))
if err != nil {
log.Printf("Unable to perform POST request:\n%v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(body))
}
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报