第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

json.Marshal 用于帶有 echo 的 http post 請求

json.Marshal 用于帶有 echo 的 http post 請求

Go
湖上湖 2022-07-18 16:07:31
我有兩個在 localhost 上運行的 golang 服務(wù)器。他們使用不同的端口。我想在一個向另一個發(fā)送 JSON 對象的請求上創(chuàng)建一個發(fā)布請求。我正在使用 echo 框架(如果這很重要)我得到的錯誤是當(dāng)我嘗試為 post 對象編組對象時:2-valued json.Marshal(data) (value of type ([]byte, error)) where single value is expected服務(wù)器 1:type SendEmail struct {    SenderName       string `json:"senderName,omitempty" bson:"senderName,omitempty" validate:"required,min=3,max=128"`    SenderEmail      string `json:"senderEmail" bson:"senderEmail" validate:"required,min=10,max=128"`    Subject          string `json:"subject" bson:"subject" validate:"required,min=10,max=128"`    RecipientName    string `json:"recipientName" bson:"recipientName" validate:"required,min=3,max=128"`    RecipientEmail   string `json:"recipientEmail" bson:"recipientEmail" validate:"required,min=10,max=128"`    PlainTextContent string `json:"plainTextContent" bson:"plainTextContent" validate:"required,min=10,max=512"`}func resetPassword(c echo.Context) error {email := c.Param("email")    if email == "" {        return c.String(http.StatusNotFound, "You have not supplied a valid email")    }    data := SendEmail{        RecipientEmail:   email,        RecipientName:    email,        SenderEmail:      “test@test”,        SenderName:       “name”,        Subject:          "Reset Password",        PlainTextContent: "Here is your code to reset your password, if you did not request this email then please ignore.",    }// error here    req, err := http.NewRequest("POST", "127.0.0.1:8081/", json.Marshal(data))    if err != nil {        fmt.Println(err)    }    defer req.Body.Close()    return c.JSON(http.StatusOK, email)}服務(wù)器 2:e.GET("/", defaultRoute)func defaultRoute(c echo.Context) (err error) {    u := SendEmail{}    if err = c.Bind(u); err != nil {        return    }    return c.JSON(http.StatusOK, u)}
查看完整描述

2 回答

?
慕仙森

TA貢獻1827條經(jīng)驗 獲得超8個贊

見到 Gopher 總是很高興。您可能想知道一些事情,Go 支持多值返回,因為一個函數(shù)可以返回多個值。


byteInfo, err := json.Marshal(data) // has two values returned

// check if there was an error returned first

if err != nil{

  // handle your error here

}

現(xiàn)在在你的代碼下面的行


// error here

req, err := http.NewRequest("POST", "127.0.0.1:8081/", json.Marshal(data))

會變成這個


// error here

req, err := http.NewRequest("POST", "127.0.0.1:8081/", bytes.NewBuffer(byteInfo))

您可以繼續(xù)使用其余代碼??鞓肪幋a!


查看完整回答
反對 回復(fù) 2022-07-18
?
慕婉清6462132

TA貢獻1804條經(jīng)驗 獲得超2個贊

json.Marshal返回[]byte,error這意味著您將 4 個值傳遞給http.NewRequest.


您應(yīng)該json.Marshal先調(diào)用,然后將結(jié)果用于http.NewRequest.


body, err := json.Marshal(data)

if err != nil {

 // deal with error

}

req, err := http.NewRequest("POST", "127.0.0.1:8081/", body)


查看完整回答
反對 回復(fù) 2022-07-18
  • 2 回答
  • 0 關(guān)注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號