获取get url参数
package main
import(
"fmt"
"net/http"
"strings"
)
//http://192.168.186.129:8888/?path=123&name=qwe
func sayHelloName(w http.ResponseWriter, r *http.Request){
r.ParseForm()
fmt.Println(r.Form)
fmt.Println("path",r.URL.Path)
fmt.Println("scheme",r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println(k)
fmt.Println(strings.Join(v,""))
}
fmt.Fprintf(w,"hello xx")
}
func main(){
http.HandleFunc("/hello",func(w http.ResponseWriter, req *http.Request){
w.Write([]byte("hello"))
fmt.Println("ok")
})
http.HandleFunc("/",sayHelloName)
http.ListenAndServe(":8888",nil)
}
测试post请求
package main
import(
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func main(){
v := url.Values{}
v.Set("mobile","18211222233")
body := ioutil.NopCloser(strings.NewReader(v.Encode()))
client := &http.Client{}
request,err := http.NewRequest("POST","https://passport.baidu.com/?getpassusertype&tt=16522112233",body)
if err != nil {
fmt.Println("Fatal err",err.Error())
}
request.Header.Set("Content-Type","application/x-www-form-urlencoded;param=value");
resp,err := client.Do(request)
content,err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Fatal error")
}
fmt.Println(string(content))
}
测试post 表单提交
package main
import(
"fmt"
"log"
"html/template"
"net/http"
"strconv"
"regexp"
)
func main(){
http.HandleFunc("/login",login)
err := http.ListenAndServe("0.0.0.0:8888",nil)
if err != nil {
log.Fatal("listenandserve",err)
}
}
func login(w http.ResponseWriter,r *http.Request){
fmt.Println("method:",r.Method)
if r.Method == "GET" {
t,_:=template.ParseFiles("login.html")
t.Execute(w,nil)
}else{
fmt.Println(r.PostFormValue("username"))
fmt.Println(r.PostFormValue("password"))
username := r.FormValue("username")
password := r.FormValue("password")
phone := r.FormValue("phone")
like := r.FormValue("like")
sex := r.FormValue("sex")
utype := r.FormValue("utype")
age := r.FormValue("age")
fmt.Println(like)
fmt.Println(sex)
fmt.Println(utype)
tage,err := strconv.Atoi(age)
if err != nil {
w.Write([]byte("数字转换错误,填写的不是数字"))
return
}
if username == "" || password == "" || tage == 0 {
w.Write([]byte("username password age must not null"))
return
}
if tage > 100 {
w.Write([]byte("age is to big"))
return
}
if m,_ := regexp.MatchString(`^(17622123322)$`,phone);!m{
w.Write([]byte("phone is error"))
return
}
w.Write([]byte("ok"))
fmt.Println("to ok")
}
}
login.html
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
<br>用户名:<input type="text" name="username"></br>
<br>密码:<input type="text" name="password"></br>
<br>phone:<input type="text" name="phone"></br>
<br>like:<input type="text" name="like"></br>
<br>sex:<input type="text" name="sex"></br>
<br>utype:<input type="text" name="utype"></br>
<br>age:<input type="text" name="age"></br>
<input type="submit" value="登录">
</form>
</body>
</html>
测试post文件上传
package main
import(
"fmt"
"net/http"
"html/template"
"log"
"io"
"os"
)
func upload(w http.ResponseWriter,r * http.Request){
if r.Method == "GET" {
t,_ := template.ParseFiles("upload.html")
t.Execute(w,nil)
}else if r.Method == "POST" {
r.ParseMultipartForm(32 << 20)
file,handle,err := r.FormFile("file")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
f,err := os.OpenFile("./upload/"+handle.Filename,os.O_WRONLY|os.O_CREATE,0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f,file)
fmt.Printf("%v\n",handle.Header)
fmt.Println("上传成功")
} else {
fmt.Println("error")
}
}
func main(){
http.HandleFunc("/upload",upload)
err := http.ListenAndServe("0.0.0.0:8888",nil)
if err != nil {
log.Fatal("listenandServe:",err)
}
}
upload.html
<html>
<head>
<title>file</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
<br><input type="file" name="file"></br>
<br><input type="submit" value="commit"></br>
</form>
</body>
</html>