-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Feature request: support Yaml responses transparently. #2594
Labels
Comments
Sending responses in Echo is 1 method call on context ala func Yaml(c echo.Context, code int, i interface{}) error {
c.Response().Status = code
c.Response().Header().Set(echo.HeaderContentType, "text/yaml")
return yaml.NewEncoder(c.Response()).Encode(i)
} and you would use that as but adding full example: import (
"errors"
"github.com/labstack/echo/v4"
"gopkg.in/yaml.v3"
"log"
"net/http"
)
// curl -v -X POST https://localhost:8080/yaml -H 'Content-Type: application/json' -d '{"name":"test","data":{"x":true}}'
func Yaml(c echo.Context, code int, i interface{}) error {
c.Response().Status = code
c.Response().Header().Set(echo.HeaderContentType, "text/yaml")
return yaml.NewEncoder(c.Response()).Encode(i)
}
type data struct {
Name string `yaml:"name" json:"name"`
Data map[string]interface{} `yaml:"data" json:"data"`
}
func main() {
e := echo.New()
e.POST("/yaml", func(c echo.Context) error {
var payload data
if err := c.Bind(&payload); err != nil {
return err
}
return Yaml(c, http.StatusCreated, payload)
})
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
} curl -v -X POST https://localhost:8080/yaml -H 'Content-Type: application/json' -d '{"name":"test","data":{"x":true}}' Output: x@x:~/$ curl -v -X POST https://localhost:8080/yaml -H 'Content-Type: application/json' -d '{"name":"test","data":{"x":true}}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* processing: https://localhost:8080/yaml
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> POST /yaml HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.2.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 33
>
< HTTP/1.1 201 Created
< Content-Type: text/yaml
< Date: Wed, 14 Feb 2024 18:08:41 GMT
< Content-Length: 29
<
name: test
data:
x: true
* Connection #0 to host localhost left intact |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used echo as an API gateway in front of some kubernetes resources, being able to return responses rendered as Yaml would have been handy.
Kubernetes API machinery produces Yaml off of the Json tags. Could we do something like that here ? The API wouldn't have to change.
Supporting it similarly on the request side (
Bind
) would also be great but perhaps for another issue.This issue is related.
The text was updated successfully, but these errors were encountered: