0

我有一个用 Go/Gin 编写的服务,我接受 multipart/form-data 以接受来自用户的文件列表。代码看起来像这样。

func UploadScreenshotsHandler(c *gin.Context) {
    //Multipart form
    form, err := c.MultipartForm()
    if err != nil {
        logger.Log.Error(err.Error())
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    files := form.File
    // more logics with the files
}

我正在使用 GoDoc 来记录我的端点。那么如何指定我的 godoc 以便 swagger 知道这是一个接受 multipart/form-data 的端点?

// UploadFileHandler godoc
// @Summary Upload a list of file 
// @Description An API for clients to upload files
// @Param <WHAT DO I PUT HERE??> <----------
// @Success 200 "Successfully uploaded"
// @Failure 400 {object} interface{} "Bad request: request body has to be form-data"
// @Router / [post]
4

1 回答 1

1

指定端点接收多个文件

// UploadFile godoc
// @Summary Uploads a file
// @Description Takes in a file as part of a multipart form and stores the file
// @Tags Uploads
// @Accept  multipart/form-data
// @Produce  json
// @Param file formData file true "File 1 to upload"
// @Param file1 formData file true "File 2 to upload"
// @Success 200 "Successfully uploaded"
// @Failure 400 {object} interface{} "Bad request: request body has to be form-data"
// @Router / [post]

func UploadHandler(c *gin.Context) {
   ...
}
于 2021-02-26T16:34:06.690 回答