Home >Backend Development >Golang >golang request parameter format
In Golang, we often need to use HTTP protocol for data interaction. In HTTP requests, request parameters are very common, so the correct request parameter format is very important for backend developers.
So, what are the request parameter formats in Golang? The following will be introduced in detail through code examples.
Form request parameter is one of the most common request parameter forms. In normal scenarios, we will use POST requests to send form data, and the request parameters will be encapsulated in the request body.
The following is a sample code using the net/http
library:
package main import ( "log" "net/http" ) func main() { http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { username := r.PostFormValue("username") password := r.PostFormValue("password") log.Printf("username: %s, password: %s", username, password) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
In the above example, we pass the r.PostFormValue()
method to get the parameters in the form. This method will automatically parse the form parameters in the request body and store them in a map.
In addition to form form request parameters, another common request parameter form is JSON. In RESTful APIs, request parameters in JSON format have become an industry standard.
Next we use the encoding/json
library to parse the request parameters in JSON format:
package main import ( "encoding/json" "log" "net/http" ) type User struct { Username string `json:"username"` Password string `json:"password"` } func main() { http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { var user User err := json.NewDecoder(r.Body).Decode(&user) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } log.Printf("username: %s, password: %s", user.Username, user.Password) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
In the above example, we first define a User
Structure, and then use the json.NewDecoder()
method to parse the JSON data in the request body. After parsing, we can easily obtain the actual data submitted by the user.
Query parameters are parameters added directly after the URL, for example: http://example.com/path?name=value
. Common query operations are completed through Query parameters. In Golang, we can use the net/url
library to parse Query parameters:
package main import ( "log" "net/http" "net/url" ) func main() { http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() name := query.Get("name") minPrice := query.Get("minPrice") maxPrice := query.Get("maxPrice") log.Printf("name: %s, minPrice: %s, maxPrice: %s", name, minPrice, maxPrice) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
In the above example, we use r.URL.Query()
The method obtains the query parameters behind the URL, and uses the Get()
method to obtain the value of the corresponding parameter.
Path parameter is a parameter directly added to the URL path, for example: http://example.com/path/{name}
. In Golang, we can use the net/http
library with regular expressions to get the Path parameters:
package main import ( "log" "net/http" "regexp" ) func main() { http.HandleFunc("/users/", func(w http.ResponseWriter, r *http.Request) { re := regexp.MustCompile(`/users/(d+)`) match := re.FindStringSubmatch(r.URL.Path) if match == nil { http.NotFound(w, r) return } id := match[1] log.Printf("user id: %s", id) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
In the above example, we use the regular expression /users/ (d)
to match the numbers in the URL path, and obtain the matching results through the FindStringSubmatch()
method. In this way, we can easily get the Path parameter.
The above are the common formats and examples of request parameters in Golang. According to our actual needs, choosing the appropriate request parameter format for data transmission can make our application more efficient and stable.
The above is the detailed content of golang request parameter format. For more information, please follow other related articles on the PHP Chinese website!