当前位置:网站首页>【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
2022-07-02 06:33:00 【菜鸟实战】
目录
一、简介
实战场景: 如何使用 gin 获取 GET 和 POST 的请求参数
二、知识点
- gin 路由
- http 状态码
- 获取 GET 请求参数
- 获取 POST 请求参数
- 获取 POST Form 请求参数
三、菜鸟实战
马上安排!
1、创建 go文件
package main
// 导入包
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"runtime"
)
// 主函数
func main() {
// 使用内置函数打印
println("Hello", "菜鸟实战")
println("实战场景: ", "gin 如何获取 GET 请求参数")
// 初始化
r := gin.Default()
// GET 方法, 添加路由参数和回调
r.GET("/g", func(c *gin.Context) {
name := c.Query("name")
// 默认参数
age := c.DefaultQuery("age", "18")
// 返回的 code 和 对应的参数星系
c.String(http.StatusOK, "%s - %s \n", name, age)
})
// POST 方法, 添加路由参数和回调
r.POST("/p", func(c *gin.Context) {
bodyByts, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
// 返回错误信息
c.String(http.StatusBadRequest, err.Error())
// 执行退出
c.Abort()
}
// 返回的 code 和 对应的参数星系
c.String(http.StatusOK, "%s \n", string(bodyByts))
})
// POST 方法, 添加路由参数和回调, 填充参数
r.POST("/p1", func(c *gin.Context) {
bodyByts, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
// 返回错误信息
c.String(http.StatusBadRequest, err.Error())
// 执行退出
c.Abort()
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyByts))
name := c.PostForm("name")
age := c.DefaultPostForm("age", "18")
// 返回的 code 和 对应的参数星系
c.String(http.StatusOK, "%s,%s,%s\n", name, age, string(bodyByts))
})
// 使用包函数打印
fmt.Printf("版本: %s \n", runtime.Version())
// 启动框架程序, 默认 8080 端口
r.Run()
}
// curl 验证方式
// g
// curl -X GET "http://127.0.0.1:8080/g?name=kitty"
// curl -X GET "http://127.0.0.1:8080/g?name=kitty&age=21"
// p
// curl -X POST "http://127.0.0.1:8080/p" -d '{"name":"你好, 菜鸟实战"}'
// p1
// curl -X POST "http://127.0.0.1:8080/p1" -d 'name=bob'
// curl -X POST "http://127.0.0.1:8080/p1" -d 'name=bob&age=17'
2、运行结果
Hello 菜鸟实战
实战场景: gin 如何获取 GET 和 POST 的请求参数
[GIN-debug] Listening and serving HTTP on :8080
GET 方法, 添加路由参数和回调
POST 方法, json格式
POST 方法, 添加路由参数和回调, 填充参数
菜鸟实战,持续学习!
边栏推荐
- 1、 QT's core class QObject
- Gateway is easy to use
- Sqli labs Level 2
- 一个经典约瑟夫问题的分析与解答
- Installing Oracle database 19C for Linux
- Minecraft空岛服开服
- Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
- KubeSphere 虚拟化 KSV 安装体验
- commands out of sync. did you run multiple statements at once
- zipkin 简单使用
猜你喜欢
Sentinel 简单使用
CSDN Q & A_ Evaluation
Sqli labs level 1
Getting started with k8s: building MySQL with Helm
Installing Oracle database 19C for Linux
Sqli labs Level 2
Openshift deployment application
Minecraft install resource pack
win10使用docker拉取redis镜像报错read-only file system: unknown
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
随机推荐
C# 高德地图 根据经纬度获取地址
汉诺塔问题的求解与分析
Openshift deployment application
Npoi export word font size correspondence
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
使用递归函数求解字符串的逆置问题
Zipkin is easy to use
C # save web pages as pictures (using WebBrowser)
Gateway is easy to use
kubeadm部署kubernetes v1.23.5集群
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
Sqli labs level 12
WSL installation, beautification, network agent and remote development
C#钉钉开发:取得所有员工通讯录和发送工作通知
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Image transformation, transpose
QT -- how to set shadow effect in QWidget
Leetcode sword finger offer brush questions - day 22
C# 调用系统声音 嘀~
Gocv boundary fill