How to set request body by entoas? #3585
Replies: 2 comments
-
Be nice to get answers to questions like this and with more examples. This is a great framework with lots of potential but the documentation and tutorials are really lacking. |
Beta Was this translation helpful? Give feedback.
-
I came across this today while searching for an answer. I didn't find a better one, so I thought I'd double back and leave what I came up with in case anyone has any notes or suggestions (or if a future someone has the same question and find this too). // In entc.go, creating mutations
req := ogen.NewSchema().
SetType("object").
AddRequiredProperties(ogen.NewProperty().SetName("id").SetSchema(ogen.Int())).
AddOptionalProperties(ogen.NewProperty().SetName("title").SetSchema(ogen.String()))
resp := ogen.NewSchema().
SetType("object").
AddRequiredProperties(ogen.NewProperty().SetName("response").SetSchema(ogen.String())).
AddOptionalProperties(ogen.NewProperty().SetName("now").SetSchema(ogen.DateTime()))
post := ogen.NewOperation().
SetRequestBody(ogen.NewRequestBody().SetJSONContent(req)).
AddResponse("200", ogen.NewResponse().AddContent(ir.EncodingJSON.String(), resp))
test := ogen.NewPathItem().SetPost(post)
s.AddPathItem("/test", test) Note that the And then, in my main.go (or wherever you're starting the server: // TestPost implements ogent.Handler.
func (handler) TestPost(ctx context.Context, req ogent.OptTestPostReq) (*ogent.TestPostOK, error) {
if !req.IsSet() {
log.Fatalln("Didn't set the values...")
}
log.Println(req.Value.Title)
return &ogent.TestPostOK{
Response: "OK!",
Now: ogent.NewOptDateTime(time.Now()),
}, nil
} |
Beta Was this translation helpful? Give feedback.
-
I know how to set param(path paramater,query paramater) from this doc --> https://entgo.io/ja/blog/2022/02/15/generate-rest-crud-with-ent-and-ogen#%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0[…]%E3%83%88%E3%82%92%E8%BF%BD%E5%8A%A0
But, I can not found, how to set request body.
I try to write below cord. but not working.
Please tell me how to set request body by entoas.
Beta Was this translation helpful? Give feedback.
All reactions