savaki / swag

Simple go wrapper to create swagger compatible APIs
Apache License 2.0
85 stars 45 forks source link

Describing POST handler for multipart/form-data #5

Open ainvaltin opened 7 years ago

ainvaltin commented 7 years ago

How one is supposed to describe endpoint for a html form which posts multipart/form-data? It seems that the endpoint.Body() func is meant to be used to describe the fields but I can't figure out what the first parameter should be. Say I have a form which posts an file and a string field...

As a workaround, following seems to work:

api.AddEndpoint(endpoint.New(http.MethodPost, "/foo", "handle posted data",
    endpoint.Handler(handle_post),
    parameter(swagger.Parameter{In: "formData", Type: "file", Name: "file", Description: "file to process", Required: true}),
    parameter(swagger.Parameter{In: "formData", Type: "string", Name: "op", Description: "name of the operation", Required: true}),
    endpoint.Consumes("multipart/form-data"),
    endpoint.Produces("text/xml; charset=utf-8"),
    endpoint.Response(http.StatusOK, struct{}{}, "desc response"),
))

the parameter func is one from endpoint/buider.go.

If there is better way please document it (in some example or as a code snippet in readme).

MarkSonghurst commented 7 years ago

I think #4 does what you need.