Golang net/http.Request.FormFile() and FormValue() functions example
package net/http
Golang net/http.Request.FormFile() and FormValue() functions usage example
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("file") // <-------------------------------- here!
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
out, err := os.Create("/tmp/uploadedfile")
if err != nil {
fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
return
}
defer out.Close()
// write the content from POST to the file
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintf(w, "File uploaded successfully : ")
fmt.Fprintf(w, header.Filename)
}
and
func process_form_data(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
form_data := r.FormValue("form_data")
}
}
References :
https://www.socketloop.com/tutorials/get-form-post-value-go
http://golang.org/pkg/net/http/#Request.FormFile
Advertisement
Something interesting
Tutorials
+6k Golang : Function as an argument type example
+11.1k Golang : Web routing/multiplex example
+8.5k Linux/Unix : fatal: the Postfix mail system is already running
+9.9k Golang : Turn string or text file into slice example
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+6.3k Golang : How to search a list of records or data structures
+19.4k Golang : Fix cannot download, $GOPATH not set error
+5.8k Golang : List all packages and search for certain package
+24.5k Golang : How to validate URL the right way
+5.9k Golang : Generate multiplication table from an integer example
+9.2k Golang : Generate Codabar
+10.6k Golang : ISO8601 Duration Parser example