Accessing Query Strings from POST Requests in Go's HTTP Package
When working with HTTP POST requests in Go, it's common to need to access the query string. The query string is a portion of the request URL that contains additional data in the format of key-value pairs.
Solution:
To access the query string from a POST request using Go's HTTP package, you can utilize the Query method of the http.Request object:
func newHandler(w http.ResponseWriter, r *http.Request) { queries := r.URL.Query() // Access individual query parameters param1 := queries.Get("param1") // Access multiple values associated with a key param1s := queries["param1"] }
Example:
For a request with the URL http://example.com/path?param1=value1¶m2=value2, the following code will retrieve the corresponding values:
queries := r.URL.Query() param1 := queries.Get("param1") // "value1" param2 := queries.Get("param2") // "value2"
Note:
The above is the detailed content of How Can I Access Query Strings from POST Requests in Go?. For more information, please follow other related articles on the PHP Chinese website!