当前第2页 返回上一页
一、Golang接收前端GET请求的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | func handleGet(writer http.ResponseWriter, request *http.Request) {
query := request.URL.Query()
id := query.Get( "id" )
fmt.Printf( "GET: id=%s\n" , id)
fmt. Fprintf (writer, `{ "code" :0}`)
}
func main() {
http.HandleFunc( "/testGet" , handleGet)
}
|
服务端打印如下:
二、Golang接收前端POST请求的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import (
"encoding/json"
)
func handlePostJson(writer http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
var params map[string]string
decoder.Decode(¶ms)
fmt.Printf( "POST json: username=%s, password=%s\n" , params[ "username" ], params[ "password" ])
fmt. Fprintf (writer, `{ "code" :0}`)
}
func main() {
http.HandleFunc( "/testPostJson" , handlePostJson)
}
|
服务端打印如下:
1 | POST json: username=admin, password=123
|
更多golang知识请关注PHP中文网golang教程栏目。
以上就是Golang如何接收前端的参数的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
深入理解go的interface
golang不适合开发web吗
go 字符串常用的系统函数
手撸golang go与微服务 saga模式之1
gtpl go语言html渲染
golang中的map是结构体吗
golang如何创建map
适合开发者的七种python代码审查工具
golang和go是什么关系
kubeedge v1.3部署指南
更多相关阅读请进入《golang》频道 >>
老貘
一个与时俱进的Go编程知识库。
转载请注明出处:木庄网络博客 » Golang如何接收前端的参数