Git Product home page Git Product logo

Comments (3)

RedCrazyGhost avatar RedCrazyGhost commented on May 10, 2024 1

According to what you said, I processed the code, you can take a look at my code below:

package main

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

func main() {
	g := gin.Default()

	g.Use(func(c *gin.Context) {
		body, err := c.GetRawData()
		if err != nil {
			c.Abort()
			c.JSON(http.StatusInternalServerError,errors.New("body handling exception"))
		} else {
			c.Set(gin.BodyBytesKey,body)
		}
	})

	g.POST("/hello", func(c *gin.Context) {
		type helloBody struct {
			Name string `json:"name"`
		}
		var body helloBody
		
		err := c.ShouldBindBodyWith(&body,binding.JSON)
		if err != nil {
			return 
		}
		
		c.String(200, body.Name)

	})
	g.Run(":9000")
}

Storing the Body into context negatively affects processing in highly concurrent scenarios and requires you to manually handle the Content-Type yourself.

NOTE: This method reads the body before binding. So you should use ShouldBindWith for better performance if you need to call only once.

It is recommended to use the official default BodyBytesKey, and it is also possible to store your own key in context.

// BodyBytesKey indicates a default body bytes key.
const BodyBytesKey = "_gin-gonic/gin/bodybyteskey"

from gin.

dbhoot avatar dbhoot commented on May 10, 2024

My way forward is to call c.ShouldBindBodyWith(...) in my middleware and then c.Get(gin.BodyBytesKey) to get the body. Then later in the actual handler function, I use c.ShouldBindBodyWith(...) again to actually use the body.

from gin.

dbhoot avatar dbhoot commented on May 10, 2024

Thanks. I read through the source and that is indeed how I solved the problem.

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.