Home > Backend Development > Golang > How Do I Access Query String Parameters in Go's POST Requests?

How Do I Access Query String Parameters in Go's POST Requests?

Mary-Kate Olsen
Release: 2024-12-18 21:52:11
Original
104 people have browsed it

How Do I Access Query String Parameters in Go's POST Requests?

Accessing Query String Parameters in Go's POST Requests

In Golang's http package, when handling POST requests, accessing the query string can be done by parsing the Request object's query parameters. The Request type provides the Query method, which returns a Values map containing the key-value pairs of the query string.

Example

Consider a POST request with a URL like:

http://host:port/something?param1=b
Copy after login

To access the GET parameters in Go:

func newHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Println("GET params were:", r.URL.Query())

  // Get a single parameter
  param1 := r.URL.Query().Get("param1")

  // Get multiple parameters or empty values
  param1s := r.URL.Query()["param1"]
}
Copy after login

Note:

  • Query keys are case-sensitive.
  • If multiple values are provided for a single key, they will be returned as a slice in the Values map.

The above is the detailed content of How Do I Access Query String Parameters in Go's POST Requests?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template