mux icon indicating copy to clipboard operation
mux copied to clipboard

Add Body Input / Output to Route [feature]

Open deBarrosS opened this issue 2 years ago • 2 comments

Description of the solution I'd like

How would it work? How would it change the API?

By walking the Router we are able to Get Routes' PathTemplates, QueriesTemplates, etc. But we are not able(at least I haven't been) to get the body accepted by the request neither the possible responses' bodies. This could really come in handy when automating the writing of API Documentations as well as validating the requests received.

By adding the functions like BodyInput( ) and BodyOutput( ) to a Route, we'd be able to set Input/Output body parameters (add Headers as well?). Therefore, by adding a function GetBodyInput() and GetBodyOutput() it'd be possible to read them. To the output nevertheless, the question to be made is : do we only set the 200 status response or do we set a map with codes as keys?

Alternatives I've considered

As to the alternatives, I'm trying to create an struct containing the routes, the parameters and the outputs to gaether them all in one place, but it seems to me that I'd be quite usefull to have them set on the Route itself.

deBarrosS avatar May 30 '22 09:05 deBarrosS

you can try https://github.com/hyahm/xmux example:

package main

import (
	"fmt"
	"net/http"

	"github.com/hyahm/xmux"
)

type R struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
}

type User struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

func main() {
	router := xmux.NewRouter()
	router.BindResponse(&R{})
        // router.Exit
	router.Post("/login", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println(string(xmux.GetInstance(r).Body))
	}).BindJson(User{})
	router.Run()
}

when you request

curl -X 'POST' -d '
{
    "username": "foo",
    "password": "bar"
}'  http://localhost:8080/login

in console you can see the log

{
    "username": "foo",
    "password": "bar"
}
2022/06/17 09:31:04 connect_id: 1655429464276835500,method: POST        url: /login     time: 0.000975   status_code: 200, response: {"code":0,"data":null}

For more custom logs please use router.Exit to modify

hyahm avatar Jun 14 '22 09:06 hyahm

By adding the functions like BodyInput( ) and BodyOutput( ) to a Route, we'd be able to set Input/Output body parameters (add Headers as well?)

@deBarrosS could you be a bit more specific what are you trying to say with an example?

For adding headers as a part of request or response you can use middleware. If you haven't checked it before I would request you to please take a look at the project: https://github.com/gorilla/handlers/tree/master

HTTP status are the part of net/http package. Here is the link of all the HTTP status codes: https://go.dev/src/net/http/status.go

do we only set the 200 status response or do we set a map with codes as keys?

Again couldn't understand what are you trying to say here?

amustaque97 avatar Jun 16 '22 16:06 amustaque97