[英]Post Marshaled JSON data as URL Encoded Form Data
我正在嘗試通過將身份驗證結構轉換為application/x-www-form-urlencoded
數據來發送 POST 請求。
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type Payload struct {
Username string `json:"username"`
Password string `json:"password"`
GrantType string `json:"grant_type"`
Scope string `json:"scope"`
}
func main() {
var endpoint string = "https://api.io/v1/oauth/token"
jsonPay := &Payload{
Username: "email",
Password: "pass",
GrantType: "password",
Scope: "SPACE SEPARATED STRINGS",
}
//byteArr, err := json.Marshal(jsonPay)
//if err != nil {
// log.Printf("Unable to map structure\n%v", err)
//}
payloadBuf := new(bytes.Buffer)
json.NewEncoder(payloadBuf).Encode(jsonPay)
req, err := http.NewRequest("POST", endpoint, payloadBuf)
if err != nil {
log.Fatal(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.Printf(string(body))
}
我試過了:
發送 JSON 編碼的有效負載緩沖區,該緩沖區返回
{"error":"invalid_request","error_description":"Missing grant type"}
將bytes.NewReader
與封送處理的 JSON 對象一起使用,該對象也返回
{"error":"invalid_request","error_description":"Missing grant type"}
將strings.NewReader
與 JSON 編碼的有效負載緩沖區一起使用,該緩沖區返回
cannot use payloadBuf (variable of type *bytes.Buffer) as type string in argument to strings.NewReader
curl 請求如下所示:
curl --request POST \
--url https://api.io/v1/oauth/token \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data 'username=email' \
--data 'password=pass' \
--data 'grant_type=password' \
--data 'scope=SPACE SEPARATED STRINGS'
並與:
package main
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
func main() {
const endpoint string = "https://api.io/v1/oauth/token"
payload := url.Values{
"username": {"email"},
"password": {"pass"},
"grant_type": {"password"},
"scope": {"SPACE SEPARATED STRINGS"},
}
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))
}
如何將編組的 JSON 數據發布為application/x-www-form-urlencoded
實施@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))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.