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
+5.5k Unix/Linux/MacOSx : How to remove an environment variable ?
+8k Golang Hello World Example
+11.8k Android Studio : Create custom icons for your application example
+7.6k Golang : Create zip/ePub file without compression(use Store algorithm)
+10k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+30k Golang : How to get HTTP request header information?
+10.5k Golang : cannot assign type int to value (type uint8) in range error
+10.4k Golang : How to check if a website is served via HTTPS
+5.6k Golang : Stop goroutine without channel
+6.3k Apt-get to install and uninstall Golang
+15.8k Golang : Get checkbox or extract multipart form data value example
+4.9k Golang : A program that contain another program and executes it during run-time