go语言 获取post方式json
来源:互联网 发布:mac日本官网 编辑:程序博客网 时间:2024/06/10 06:47
正在学习go语言,看了一段时间的文档,想自己写个服务器,在获取接口数据的时候发现获取数据,格式为"form-data"的数据最简单,</span>
传入post json数据:{"username":"","password":"123456"}
<span style="font-family: Arial, Helvetica, sans-serif;">req.PostForm</span>
req.Header.Get("Content-Type")req.Hostreq.Formreq.FormValue("username")req.FormValue("password")
获取"application/json"的时候,需要处理一下(以下只获取string):
package utilsimport ("bytes""io/ioutil""net/http")/**获取body的data(json)转换为string*字节数据转string*/func GetDataString(req *http.Request) string {result, err := ioutil.ReadAll(req.Body)if err != nil {return "{\"code\": 1,\"msg\": \"failed\"}"} else {return bytes.NewBuffer(result).String()}}
获取"application/json"的时候,需要处理一下(以下只获取json到map):
需要引入的包,"io/ioutil","net/http","encoding/json"
var user map[string]interface{}body, _ := ioutil.ReadAll(req.Body)json.Unmarshal(body, &user)fmt.Println("获取json中的username:", user["username"])fmt.Println("获取json中的password:", user["password"].(string)) //转字符串通过len(password)!=0判断长度
获取的数据对比:
byte[]
[123 34 117 115 101 114 110 97 109 101 34 58 34 115 121 115 116 101 109 34 44 34 112 97 115 115 119 111 114 100 34 58 34 49 50 51 52 53 54 34 125]string
{"username":"system","password":"123456"}
map
map[username:system password:123456]
0 0
- go语言 获取post方式json
- http post方式获取json参数
- flask post JSON 数据获取方式总结
- Go语言操作json
- Go语言解析JSON
- go语言JSON处理
- Go语言操作json
- go语言使用json
- Volley 重写源码,使用Post方式获取网络Json数据
- Python Web框架 flask post JSON数据获取方式总结
- C语言 如何用http post方式 上传json数据
- go提交json、获取json、解析json
- Go语言解析json字符串
- go语言解析json库
- go 语言解析复杂json
- GO语言学习:JSON处理
- go语言json操作指南
- go语言编程-json数据处理
- IMX6Solo启动流程-Linux 内核启动 六
- C/C++ 中头文件相互包含引发的问题
- 简述tcp协议对http性能的影响及优化
- GFS为什么要三个副本全写成功才算成功?——基于paxos实现多数派逻辑的方案设计
- ECharts图表入门:如何配置ECharts图表相关文件以及基本配置呈现最基本线性图表
- go语言 获取post方式json
- JavaScript之作用域与闭包详解
- 随便写写
- 励精图治---Concurrency---吞吐量、活跃性及死锁
- 允许远程用户登录访问mysql的方法
- 房地产企业城市置业 BI 实施分析
- .net mvc 一个Action的 HttpGet 和 HttpPost
- spring源码学习(1)-bean包中主要接口部分的简单介绍
- 从上往下打印二叉树