This is a creation in Article, where the information may have evolved or changed.
In the mature language Java, Python, php to get these parameters should be very simple, the newer language Golang use to get these parameters or a lot of effort, I hereby record.
Golang version: 1.3.1
Before the code, if you can understand the Golang http.request three properties of the form, Postform, Multipartform should be able to understand the code better, the following excerpt.
Form, Postform, multipartform description
Form, Postform, multipartform description
Briefly explain
Form: The post, put, and get parameters are stored, and the Parseform method needs to be called before use.
Postform: The post, put parameter is stored, and the Parseform method needs to be called before it can be used.
Multipartform: The post parameter for the form containing the file upload is stored, and the Parsemultipartform method needs to be called before use.
Get Get Parameters
A more common version of the Web is:
R.parseform ()
If Len (r.form["id"]) > 0 {
Fmt. Fprintln (W, r.form["id"][0])
}
where R represents *http. Request Type, W represents HTTP. The Responsewriter type.
R.form is the Url.values dictionary type, and r.form["id" is taken to an array type. Because Http.request will put parameters of the same name into the same array when parsing the parameters, it is used [0] to get to the first one.
This is usually not a problem, but it is not possible to fetch the desired value if the following is the request:
<form action= "http://localhost:9090/?id=1" method= "POST" >
<input type= "text" name= "id" value= "2"/>
<input type= "Submit" value= "Submit"/>
</form>
Because R. The form contains the get and post parameters, and the post parameter is first, the previous post parameter and the get parameter have an ID, so the post parameter 2 should be taken. Although this is not uncommon, it should be handled from a rigorous standpoint. Immediately fill in the improvement code:
Queryform, err: = URL. Parsequery (R.url. Rawquery)
If Err = = Nil && len (queryform["id"]) > 0 {
Fmt. Fprintln (W, queryform["id"][0])
}
The code is simple, which is the parameter after parsing the URL question mark. In fact, this is also the standard library parseform about get parameter parsing code.
Get Post Parameters
Here are two things to do:
Normal post form request, content-type=application/x-www-form-urlencoded
form with file upload, Content-type=multipart/form-data
The first case is relatively simple and can be taken directly with Postformvalue.
Fmt. Fprintln (W, r.postformvalue ("id"))
The second case is more complicated, such as the following form:
<form action= "http://localhost:9090" method= "POST" enctype= "Multipart/form-data" >
<input type= "text" name= "id" value= "2"/>
<input type= "file" name= "Pic"/>
<input type= "Submit" value= "Submit"/>
</form>
Because the file needs to be uploaded, the form enctype to be set to Multipart/form-data. The value of the ID cannot be obtained by postformvalue at this time because the method is not implemented in the Golang library:
Cannot get post parameters with Postform in Golang
Cannot get post parameters with Postform in Golang
Fortunately, another attribute multipartform can be provided in Golang to handle this situation.
R.parsemultipartform (<< 20)
If r.multipartform! = Nil {
Values: = r.multipartform.value["id"]
If Len (values) > 0 {
Fmt. Fprintln (W, Values[0])
}
}
Get cookie Parameters
Cookie, err: = R.cookie ("id")
if Err = = Nil {
FMT. Fprintln (W, "Domain:", Cookie.) Domain)
Fmt. Fprintln (W, "Expires:", Cookie. Expires)
Fmt. Fprintln (W, "Name:", Cookie.) Name)
Fmt. Fprintln (W, "Value:", Cookie.) Value)
}
R.cookie returns *HTTP. Cookie type, you can obtain data such as domain, expiration time, value, and so on.