亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將編組的 JSON 數據發布為 URL 編碼的表單數據

將編組的 JSON 數據發布為 URL 編碼的表單數據

Go
MMTTMM 2022-12-19 11:59:06
我正在嘗試通過將 auth 結構轉換為application/x-www-form-urlencoded數據來發送 POST 請求。package mainimport (    "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"}與編組的 JSON 對象一起使用bytes.NewReader,它也返回{"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
查看完整描述

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))

}



查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號