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

Add Body Input / Output to Route [feature] #677

Open
deBarrosS opened this issue May 30, 2022 · 2 comments
Open

Add Body Input / Output to Route [feature] #677

deBarrosS opened this issue May 30, 2022 · 2 comments

Comments

@deBarrosS
Copy link

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.

@hyahm
Copy link

hyahm commented Jun 14, 2022

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

@amustaque97
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants