Golang : Handle API query by curl with Gorilla Queries example
This is an add on for previous tutorial on how to use Gorilla mux example. One reader emailed and asked me on how to use Gorilla mux to handle curl query or URL that look like this :
http://website.com:8080/api/getDataAPI/v1?name=Boo&job=CEO&age=50
rather than :
http://website.com:8080/person/Boo/CEO/50
Gorilla webtoolkit has the Queries
method that you can use to configure your handler to accept input with &
. This can be useful in building REST API.
Below is an example for you to compare the way how ProcessPathVariables()
versus HandleAPI()
functions work. HandleAPI() function uses the Queries() method to break down the variables from the given URL.
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("use curl command!"))
}
func ProcessPathVariables(w http.ResponseWriter, r *http.Request) {
// break down the variables for easier assignment
vars := mux.Vars(r)
name := vars["name"]
job := vars["job"]
age := vars["age"]
w.Write([]byte(fmt.Sprintf("Name is %s ", name)))
w.Write([]byte(fmt.Sprintf("Job is %s ", job)))
w.Write([]byte(fmt.Sprintf("Age is %s ", age)))
}
func HandleAPI(w http.ResponseWriter, r *http.Request) {
// Queries will automatically break down the &variables
// you don't need to worry about the ampersand & in the
// URL.
vars := mux.Vars(r)
version := vars["version"]
name := vars["name"]
job := vars["job"]
age := vars["age"]
w.Write([]byte(fmt.Sprintf("Version is %s\n", version)))
w.Write([]byte(fmt.Sprintf("Name is %s \n", name)))
w.Write([]byte(fmt.Sprintf("Job is %s \n", job)))
w.Write([]byte(fmt.Sprintf("Age is %s \n", age)))
}
func main() {
mx := mux.NewRouter()
mx.HandleFunc("/", Home)
//to handle URL like
//http://website:8080/person/Boo/CEO/199
//http://website:8080/person/Boo/CEO/199 <- if age > 199, will cause 404 error
mx.HandleFunc("/person/{name}/{job}/{age:[0-199]+}", ProcessPathVariables)
//to handle URL like
//http://website:8080/api/getDataAPI/v1?name=Boo&job=CEO&age=50
mx.HandleFunc("/api/getDataAPI/{version}", HandleAPI).Queries("name", "{name}", "job", "{job}", "age", "{age:[0-999]+}")
http.ListenAndServe(":8080", mx)
}
Do test out this code with curl and see how it goes.
A sample output :
http://example.com:8080/api/getDataAPI/v1?name=Boo&job=CEO&age=999
Version is v1
Name is Boo
Job is CEO
Age is 999
Reference :
See also : Golang : Gorilla mux routing 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
+8.9k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+34.3k Golang : Smarter Error Handling with strings.Contains()
+7.3k Golang : Handling Yes No Quit query input
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode
+7.1k Golang : How to detect if a sentence ends with a punctuation?
+4.6k Which content-type(MIME type) to use for JSON data
+14.2k Golang : How to determine if user agent is a mobile device example
+7.7k Swift : Convert (cast) String to Float
+31.8k Golang : Validate email address with regular expression
+16.9k Golang : Find file size(disk usage) with filepath.Walk
+7.4k Golang : Command line ticker to show work in progress
+28.1k Golang : Read, Write(Create) and Delete Cookie example