Newbie here. I'm trying to build a web server with a RESTful API so that I can test a curl command for file uploads. I was able to create the web server and an endpoint for file uploads.
Here is my upload endpoint:
func Upload(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(MAX_MEMORY); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusForbidden)
}
fmt.Println("Endpoint hit: Upload")
for key, value := range r.MultipartForm.Value {
fmt.Fprintf(w, "%s:%s ", key, value)
log.Printf("%s:%s", key, value)
}
for _, fileHeaders := range r.MultipartForm.File {
for _, fileHeader := range fileHeaders {
file, _ := fileHeader.Open()
path := fmt.Sprintf("files/%s", fileHeader.Filename)
buf, _ := ioutil.ReadAll(file)
ioutil.WriteFile(path, buf, os.ModePerm)
log.Println(http.StatusOK, fmt.Sprintf("file %s has been uploaded", fileHeader.Filename))
}
}
}
This endpoint works with the following curl command:
curl -F 'data=@/path/to/file/foo.tar.gz' localhost:8080/upload
However, this curl command does not:
curl -f -s -S -T /path/to/file/foo.tar.gz http://localhost:8080/upload
curl: (22) The requested URL returned error: 405 Method Not Allowed
I need help creating an endpoint that will accept
curl -f -s -S -T /path/to/file/foo.tar.gz http://localhost:8080/upload
Thank you.
Edit: Here is my routes.go file.
package main
import (
"net/http"
"github.com/gorilla/mux"
)
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
return router
}
var routes = Routes{
Route{
"Index",
"GET",
"/",
Index,
},
Route{
"Upload",
"POST",
"/upload",
Upload,
},
}