Curl usage examples with Golang
Writing down these examples for my own future references. They are for my own usage and to show how to use curl
to test my own Golang programs.
1. curl POST with and without data
Normal POST without data:
$ curl -X POST http://localhost:8888/api/login
Normal POST with data:
$ curl -d "username=adamng&password=abc123" http://localhost:8888/api/login/
The Golang API server handling POST data source code:
package main
import (
"fmt"
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("use curl command!"))
}
func HandleLogin(w http.ResponseWriter, r *http.Request) {
// get the POST data
username := r.PostFormValue("username")
password := r.PostFormValue("password")
received := username + " " + password
fmt.Println(received)
w.Write([]byte(received))
}
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/api/login", HandleLogin)
http.ListenAndServe(":8888", nil)
}
2. curl upload file example
To upload file with curl:
$ curl -F fileUploadName=@"/path/filename.txt" http://localhost:8888/api/upload
NOTE: Pay attention to fileUploadName
, it must match the r.FormFile("fileUploadName")
.
This snippet does not save the uploaded file. See https://www.socketloop.com/tutorials/golang-command-line-file-upload-program-to-server-example for complete program.
package main
import (
"fmt"
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("use curl command!"))
}
func HandleUpload(w http.ResponseWriter, r *http.Request) {
// For complete program, see
// https://www.socketloop.com/tutorials/golang-command-line-file-upload-program-to-server-example
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("fileUploadName")
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
fmt.Println(header.Filename)
w.Write([]byte(header.Filename))
}
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/api/upload", HandleUpload)
http.ListenAndServe(":8888", nil)
}
3. curl post JSON data example
To POST with JSON data, use this parameter with curl
-H "Content-Type: application/json"
Example:
$ curl -H "Content-Type: application/json" -X POST -d '{"username":"adamng","password":"abc123"}' http://localhost:8888/api/login
The server source code to process JSON POST data by curl:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("use curl command!"))
}
func HandleJSON(w http.ResponseWriter, r *http.Request) {
// get JSON POST data
json, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
fmt.Println(string(json))
// see how to unmarshal json data
// at https://www.socketloop.com/tutorials/golang-unmarshal-json-from-http-response
// this will echo back to curl
//w.Write([]byte(string(json)))
}
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/api/login", HandleJSON)
http.ListenAndServe(":8888", nil)
}
Hope this helps! Happy coding!
References:
https://www.socketloop.com/tutorials/golang-command-line-file-upload-program-to-server-example
https://www.socketloop.com/tutorials/golang-unmarshal-json-from-http-response
See also : Golang : Handle API query by curl with Gorilla Queries example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.9k Golang : Squaring elements in array
+17.1k Golang : Multi threading or run two processes or more example
+13.7k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+8.9k Golang : Gonum standard normal random numbers example
+22.9k Golang : Print out struct values in string format
+38.8k Golang : How to iterate over a []string(array)
+14.2k Golang : On enumeration
+13.1k Android Studio : Password input and reveal password example
+5.4k Fix fatal error: evacuation not done in time problem
+16.8k Golang : Covert map/slice/array to JSON or XML format
+19.8k Golang : Check if os.Stdin input data is piped or from terminal
+10.2k Golang : Allow Cross-Origin Resource Sharing request