Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic with inconsistent content-type and body #2850

Open
Tevic opened this issue Aug 31, 2021 · 0 comments
Open

panic with inconsistent content-type and body #2850

Tevic opened this issue Aug 31, 2021 · 0 comments

Comments

@Tevic
Copy link
Contributor

Tevic commented Aug 31, 2021

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	g := gin.Default()
	g.POST("/hello/", func(c *gin.Context) {
		var body TestBody
		err := c.ShouldBind(&body)
		if err != nil {
			c.Status(http.StatusBadRequest)
		}
		c.String(http.StatusOK, body.Msg)
	})
	g.Run(":9000")
}

type TestBody struct {
	Msg  string `json:"msg"`
}



Expectations

$ curl -v http://127.0.0.1:9000/get/dst/abc/ -H 'Content-Type:application/x-protobuf' -X POST -d '{"msg":"ok"}'
400 Bad Request

Actual result

500 Internal Server Error
and server side panic

interface conversion: *main.TestResult is not protoiface.MessageV1: missing method ProtoMessage
/Users/Tevic/Work/RunTime/Go/src/runtime/iface.go:44 (0x100b124)
        getitab: panic(&TypeAssertionError{nil, typ, &inter.typ, name.name()})
/Users/Tevic/Work/RunTime/Go/src/runtime/iface.go:482 (0x100c164)
        assertE2I: return getitab(inter, t, false)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/binding/protobuf.go:29 (0x14b306e)
        protobufBinding.BindBody: if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/binding/protobuf.go:25 (0x14b3094)
        protobufBinding.Bind: return b.BindBody(buf, obj)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/context.go:703 (0x14bc7d8)
        (*Context).ShouldBindWith: return b.Bind(c.Request, obj)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/context.go:663 (0x14bc7b7)
        (*Context).ShouldBind: return c.ShouldBindWith(obj, b)
/Users/Tevic/Dropbox/WorkSpace/Code/GoProjs/ByteDance/Snippets/Gin.go:52 (0x14cf0fb)
        main.func1: c.ShouldBind(&xx)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0x14c5941)
        (*Context).Next: c.handlers[c.index](c)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:99 (0x14c592c)
        CustomRecoveryWithWriter.func1: c.Next()
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0x14c4ba6)
        (*Context).Next: c.handlers[c.index](c)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0x14c4b89)
        LoggerWithConfig.func1: c.Next()
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0x14c40dd)
        (*Context).Next: c.handlers[c.index](c)
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:489 (0x14c3d65)
        (*Engine).handleHTTPRequest: c.Next()
/Users/Tevic/Work/RunTime/Gp/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:445 (0x14c38c4)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/Users/Tevic/Work/RunTime/Go/src/net/http/server.go:2878 (0x126f7ba)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/Users/Tevic/Work/RunTime/Go/src/net/http/server.go:1929 (0x126ae67)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/Users/Tevic/Work/RunTime/Go/src/runtime/asm_amd64.s:1581 (0x10697a0)
        goexit: BYTE    $0x90   // NOP

Environment

  • go version: go version go1.17 darwin/amd64
  • gin version (or commit ref):
  • operating system:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant