杜松子酒/ golang - 空的Req身体 [英] gin/golang - Empty Req Body

查看:123
本文介绍了杜松子酒/ golang - 空的Req身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望能够从第三位读取请求主体第二方POST,但我得到空请求正文

  curl -u dumbuser:dumbuserpassword -HContent-Type:application / json-X POST --data'{events:3}'http:// localhost:8080 / events 

我的整个代码如下。任何指针赞赏!

pre $

$ b $ net $ http $
fmt
github.com/gin-gonic/gin


func main(){
router:= gin.Default ()
authorized:= router.Group(/,gin.BasicAuth(gin.Accounts {
dumbuser:dumbuserpassword,
}))
授权。 POST(/ events,events)
router.Run(:8080)
}

func事件(c * gin.Context){
fmt .Printf(%s,c.Request.Body)
c.JSON(http.StatusOK,c)
}


解决方案

这里的问题是,您打印出的字符串值 c.Request.Body ,它是接口类型 ReadCloser



你可以做些什么来满足自己的需要实际上包含你想要的身体是从 c.Request.Body 中读取一个字符串的值,然后将其打印出来。 这仅限于您的学习过程!



学习代码:

  func events(c * gin.Context){
x,_:= ioutil.ReadAll(c.Request.Body)
fmt.Printf(%s,string( x))
c.JSON(http.StatusOK,c)
}

但是,这不是您访问请求正文的方式。让我们通过使用绑定来为你解析身体。



更正确的代码:

类型E结构{
事件字符串
}

func事件(c * gin.Context){
data:=& amp; amp; ; E {}
c.Bind(data)
fmt.Println(data)
c.JSON(http.StatusOK,c)
}

这是访问数据的更正确的方法,因为它已经被解析出来了。请注意,如果您先读取正文,正如我们在学习步骤中所做的一样,那么 c.Request.Body 将被清空,因此将不会有任何内容



破碎的代码:

  func事件(c * gin.Context){
x,_:= ioutil.ReadAll(c.Request.Body)
fmt.Printf(%s,string(x))
data :=& E {}
c.Bind(data)//数据保持不变,因为c.Request.Body已经用完。
fmt.Println(data)
c.JSON(http.StatusOK,c)
}

您可能也很好奇为什么从此端点返回的JSON显示并清空 Request.Body 。这是出于同样的原因。 JSON编组方法不能序列化 ReadCloser ,所以它显示为空。


I'm new to Go and Gin, and am having trouble printing out the full request body.

I want to be able to read the request body from third party POST, but I'm getting empty request body

curl -u dumbuser:dumbuserpassword -H "Content-Type: application/json" -X POST --data '{"events": "3"}' http://localhost:8080/events

My entire code is as below. Any pointer is appreciated!

package main

import (
  "net/http"
  "fmt"
  "github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()
  authorized := router.Group("/", gin.BasicAuth(gin.Accounts{
     "dumbuser": "dumbuserpassword",
  }))
  authorized.POST("/events", events)
  router.Run(":8080")
}

func events(c *gin.Context) {
  fmt.Printf("%s", c.Request.Body)
  c.JSON(http.StatusOK, c)
}

解决方案

The problem here is that you're printing out the string value of c.Request.Body, which is of interface type ReadCloser.

What you can do to satisfy yourself that it does in fact contain the body you want is to read the value out of c.Request.Body to a string, and then print that out. This is for your learning process only!

Learning code:

func events(c *gin.Context) {
        x, _ := ioutil.ReadAll(c.Request.Body)
        fmt.Printf("%s", string(x))
        c.JSON(http.StatusOK, c)
}

However, this is not the way you should access the body of the request. Let gin do the parsing of the body for you, by using a binding.

More correct code:

type E struct {
        Events string
}

func events(c *gin.Context) {
        data := &E{}
        c.Bind(data)
        fmt.Println(data)
        c.JSON(http.StatusOK, c)
}

This is a more correct way to access the data in the body, since it will be already parsed out for you. Note that if you read the body first, as we did above in the learning step, the c.Request.Body will have been emptied, and so there will be nothing left in the body for Gin to read.

Broken code:

func events(c *gin.Context) {
    x, _ := ioutil.ReadAll(c.Request.Body)
    fmt.Printf("%s", string(x))
    data := &E{}
    c.Bind(data) // data is left unchanged because c.Request.Body has been used up.
    fmt.Println(data)
    c.JSON(http.StatusOK, c)
}

You're probably also curious why the JSON returned from this endpoint shows and empty Request.Body. This is for the same reason. The JSON Marshalling method cannot serialize a ReadCloser, and so it shows up as empty.

这篇关于杜松子酒/ golang - 空的Req身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆