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

Can not read JSON body on different middleware functions #1265

Closed
eduolalo opened this issue May 28, 2019 · 3 comments
Closed

Can not read JSON body on different middleware functions #1265

eduolalo opened this issue May 28, 2019 · 3 comments

Comments

@eduolalo
Copy link

eduolalo commented May 28, 2019

I'm working on a project that needs use many handler functions for a single path, this is because we reuse some functions in others paths, for example: making validations. In many cases we need to work with the body, but for some reason when we use ctx.ReadJSON function for second time it throws unexpected end of JSON input

Is there a way to pass the body betwen the handlers or these error is a bug?
I'm letting a litte example snipet of the way we are working:

package main

import (
    "github.com/kataras/iris"

    validator "gopkg.in/go-playground/validator.v9"
    "log"
)

// This is the spected structure of the JSON body
type niceStruct struct {
    name  string `json:"name" validate:"required"`
    phone string `json:"phone" validate:"required"`
}

// This function make validations to the body
// and continues the process
func verifyBody(ctx iris.Context) {

    var body niceStruct
    if err := ctx.ReadJSON(&body); err != nil {
	    log.Println("Error on reading body")
	    log.Println(err)
	    return
    }

    validate := validator.New()
    err := validate.Struct(body)
    if err != nil {
	    log.Println("Error on validating body")
	    log.Println(err)
	    return
    }
    ctx.Next()
}

// This function use the body info
func sayHello(ctx iris.Context) {

    var body niceStruct
    if err := ctx.ReadJSON(&body); err != nil { // this is where throws errors
	    log.Println("Error on reading body")
	    log.Println(err)
	    return
    }
    hello := "Hello " + body.name
    ctx.HTML(hello)

}
func main() {

    app := iris.New()

    app.Handle("GET", "/hello", verifyBody, sayHello)

    app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
@kataras
Copy link
Owner

kataras commented May 28, 2019

Hello,

By-default, the ctx.ReadJSON and all read functions will read the body until EOF (io.ReadAll consumes the data readen, the whole body like the net/http and all web frameworks in Go, this is a basic thing to know).

However, Iris has a DisableBodyConsumptionOnUnmarshal option: app.Run(iris.Addr(...), iris.WithoutBodyConsumptionOnUnmarshal). This will give your handlers the opportunity to read the body again and again without consuming it by the first ReadJSON.

@eduolalo
Copy link
Author

Ufff, I see now, thanks a lot for you help I'll read the whole net/http documentation.

@kataras
Copy link
Owner

kataras commented May 28, 2019

You're welcome @kalmecak, don't hesitate to ask again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants