golang讀取http的body時遇到的坑及解決
golang讀取http的body時遇到的坑
當服務端對http的body進行解析到map[string]interface{}時,會出現(xiàn)cli傳遞的是int類型,而服務端只能斷言成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 } //設置請求頭 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ù)字類型沒有對應int,解析出來都是float64
方案二:
type S struct { ProductID int `json:"product_id"` F float64 `json:"f"` } s := S{Product: 12, F: 2.7} jsonData, _ := json.Marshal(s) var m map[string]interface{} json.Unmarshal(jsonData, &m) fmt.Println(reflect.TypeOf(m["product_id"])) fmt.Println(reflect.TypeOf(m["f"])) decoder := jsoniter.NewDecoder(bytes.NewReader(jsonData)) decoder.UseNumber() decoder.Decode(&m) fmt.Println(reflect.TypeOf(m["product_id"].(json.Number).Int64)) fl, _ := m["f"].(json.Number).Float64() fmt.Println(reflect.TypeOf(fl))
golang讀取Response Body超時問題
context deadline exceeded(Client.Timeout or context cancellation while reading body)
問題描述
當使用io.copy進行對網(wǎng)絡請求的文件進行保存到本地時,在文件未完全保存時拋出此錯誤
問題原因
由于在構建http client 時指定了超時時間,即
return &http.Client{ Timeout: 60 * time.Second, }
故此,當時間超過此時間時context會結束
解決辦法
目前使用增加超時時間,暫時解決這個問題
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Go語言中html/template模塊詳細功能介紹與示例代碼
這篇文章主要介紹了Go語言中html/template模塊詳細功能介紹與示例代碼,這里說的是go 語言中自帶的包html/template里的一些基本操作,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-03-03K8s部署發(fā)布Golang應用程序的實現(xiàn)方法
本文主要介紹了K8s部署發(fā)布Golang應用程序的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2021-07-07