GithubHelp home page GithubHelp logo

JSON body request example about goyave HOT 3 CLOSED

go-goyave avatar go-goyave commented on July 4, 2024
JSON body request example

from goyave.

Comments (3)

sebastianmacias avatar sebastianmacias commented on July 4, 2024

An example of unmarshaling a JSON body to an struct would be very helpful as well.

Thanks

from goyave.

System-Glitch avatar System-Glitch commented on July 4, 2024

Hello. You don't have to do anything to handle both JSON and url-encoded forms, the framework does everything for you. The client has to set its Content-Type header so the framework can detect and parse the body correctly.

Here is an example:
http/request/echorequest/echo.go

package echorequest

import "github.com/System-Glitch/goyave/v2/validation"

var (
	Echo validation.RuleSet = validation.RuleSet{
		"text": {"required", "string", "between:3,50"},
	}
)

http/controller/hello/hello.go

package hello

import (
	"net/http"

	"github.com/System-Glitch/goyave/v2"
)

// Echo is a controller handler writing the input field "text" as a response.
// This route is validated. See "http/request/echorequest/echo.go" for more details.
func Echo(response *goyave.Response, request *goyave.Request) {
	response.String(http.StatusOK, request.String("text"))
}

http/route/route.go

package route

import (
	"goyave_template/http/controller/hello"
	"goyave_template/http/request/echorequest"

	"github.com/System-Glitch/goyave/v2"
)

// Register all the application routes. This is the main route registrer.
func Register(router *goyave.Router) {
	router.Route("POST", "/echo", hello.Echo, echorequest.Echo)
}

The request body is put into a map[string]interface{}. If you want to get this data into a struct, you'll have to create the struct and fill it "manually":

mystruct := &MyStruct{
	Field: response.String("text"),
}

Then, you can request it with the following curl command:

curl -H "Content-Type: application/json" -X POST -d '{"text":"Hello there!"}' http://localhost:8080/echo

This example is taken from the Goyave template.

from goyave.

sebastianmacias avatar sebastianmacias commented on July 4, 2024

Thank you

from goyave.

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.