Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Created June 22, 2017 16:35
Show Gist options
  • Save wingyplus/c8e73a4dbdff78ee3516c9acccbd8719 to your computer and use it in GitHub Desktop.
Save wingyplus/c8e73a4dbdff78ee3516c9acccbd8719 to your computer and use it in GitHub Desktop.
How to upload file with go-json-rest
package main
import (
"io"
"log"
"net/http"
"os"
"github.com/ant0ine/go-json-rest/rest"
)
func main() {
api := rest.NewApi()
api.Use(append(rest.DefaultCommonStack, &rest.AccessLogApacheMiddleware{})...)
router, _ := rest.MakeRouter(
rest.Post("/upload", func(w rest.ResponseWriter, r *rest.Request) {
// set maximum file size
r.ParseMultipartForm(32 << 20)
file, _, err := r.FormFile("file")
if err != nil {
log.Fatal(err)
}
// TODO: change it to open file > copy > close it
io.Copy(os.Stdout, file)
}),
)
api.SetApp(router)
http.ListenAndServe(":9090", api.MakeHandler())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment