免费资源网 – https://freexyz.cn/
目录
- golang读取http的body时遇到的坑
- cli
- server
- golang读取Response Body超时问题
- 问题描述
- 问题原因
- 解决办法
- 总结
golang读取http的body时遇到的坑
当服务端对http的body进行解析到map[string]interface{}时,会出现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)
}
打印类型,发现product_id是float64类型
原因:
json中的数字类型没有对应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进行对网络请求的文件进行保存到本地时,在文件未完全保存时抛出此错误
问题原因
由于在构建http client 时指定了超时时间,即
return &http.Client{
Timeout: 60 * time.Second,
}
故此,当时间超过此时间时context会结束
解决办法
目前使用增加超时时间,暂时解决这个问题
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
免费资源网 – https://freexyz.cn/
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)