利用golang對body進行讀取時會遇到哪些問題?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

當(dāng)服務(wù)端對http的body進行解析到map[string]interface{}時,會出現(xiàn)cli傳遞的是int類型,而服務(wù)端只能斷言成float64,而不能將接收到的本該是int類型的直接斷言為int
cli
func main(){
url:="http://127.0.0.1:8335/api/v2/submit"
myReq:= struct {
ProductId int `json:"product_id"`
Mobile string `json:"mobile"`
Content string `json:"content"`
Grade float64 `form:"grade" json:"grade"`
Image string `form:"image" json:"image"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
}{
ProductId:219,
Mobile:"15911111111",
Content: "這個軟件LOGO真丑",
Image: "www.picture.com;www.picture.com",
Longitude: 106.3037109375,
Latitude: 38.5137882595,
Grade:9.9,
}
reqByte,err:=json.Marshal(myReq)
req, err := http.NewRequest("POST", url, bytes.NewReader(reqByte))
if err != nil {
return
}
//設(shè)置請求頭
req.Header.Add("Content-Type", "application/json")
cli := http.Client{
Timeout: 45 * time.Second,
}
resp, err := cli.Do(req)
if err != nil {
return
}
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
fmt.Println(string(out))
}server
func SubmitV2(c *gin.Context) {
resp := &dto.Response{}
obj:=make(map[string]interface{})
var buf []byte
var err error
buf, err =ioutil.ReadAll(c. Request.Body)
if err!=nil {
return
}
err=json.Unmarshal(buf,&obj)
if err!=nil {
return
}
fmt.Println("product_id:",reflect.TypeOf(obj["product_id"]))
fmt.Println("image:",reflect.TypeOf(obj["image"]))
fmt.Println(obj)
productId:=obj["product_id"].(float64)
//注意,這里斷言成int類型會出錯
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
if !checkProduct(int(productId)){
resp.Code = -1
resp.Message = "xxxxxx"
c.JSON(http.StatusOK, resp)
return
}
url := config.Optional.OpinionHost + "/api/v1/submit"
err = http_utils.PostAndUnmarshal(url, c.Request.Body, nil, resp)
if err != nil {
logrus.WithError(err).Errorln("Submit: error")
resp.Code = -1
resp.Message = "Submit"
}
c.JSON(http.StatusOK, resp)
}打印類型,發(fā)現(xiàn)product_id是float64類型
原因:json中的數(shù)字類型沒有對應(yīng)int,解析出來都是float64
補充:Golang Web 獲取 http 請求報文主體 body 的內(nèi)容
示例代碼:
package main
import (
"fmt"
"net/http"
)
func headerBody(rw http.ResponseWriter, r *http.Request) {
// 獲取請求報文的內(nèi)容長度
len := r.ContentLength
// 新建一個字節(jié)切片,長度與請求報文的內(nèi)容長度相同
body := make([]byte, len)
// 讀取 r 的請求主體,并將具體內(nèi)容讀入 body 中
r.Body.Read(body)
// 將字節(jié)切片內(nèi)容寫入相應(yīng)報文
fmt.Fprintln(rw, body)
}
func main() {
server := http.Server{
Addr: "127.0.0.1:http",
}
http.HandleFunc("/", headerBody)
server.ListenAndServe()
}注意:
1. get 請求不包含報文主體。
2. post 請求不包含報文主體。
看完上述內(nèi)容,你們掌握利用golang對body進行讀取時會遇到哪些問題的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前名稱:利用golang對body進行讀取時會遇到哪些問題-創(chuàng)新互聯(lián)
文章位置:http://www.chinadenli.net/article42/dcseec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)頁設(shè)計公司、App設(shè)計、建站公司、網(wǎng)站制作、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容