当前位置:网站首页>【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 方法, 添加路由参数和回调, 填充参数
菜鸟实战,持续学习!
边栏推荐
- Linux安装Oracle Database 19c RAC
- Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
- Pclpy projection filter -- projection of point cloud to cylinder
- 图像变换,转置
- Qunhui NAS configuring iSCSI storage
- Openshift container platform community okd 4.10.0 deployment
- C# 高德地图 根据经纬度获取地址
- Sqli labs level 8 (Boolean blind note)
- Sentinel 简单使用
- Webflux responsive programming
猜你喜欢

Minecraft安装资源包

Nacos 下载启动、配置 MySQL 数据库

Data asset management function

commands out of sync. did you run multiple statements at once

How to realize asynchronous programming in a synchronous way?

commands out of sync. did you run multiple statements at once

Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`

QT -- how to set shadow effect in QWidget

kubernetes部署loki日志系统

MYSQL安装出现问题(The service already exists)
随机推荐
Qt的connect函数和disconnect函数
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
commands out of sync. did you run multiple statements at once
一、Qt的核心类QObject
WSL安装、美化、网络代理和远程开发
OpenShift 容器平台社区版 OKD 4.10.0部署
Sentinel 简单使用
Nacos download, start and configure MySQL database
图像变换,转置
MYSQL安装出现问题(The service already exists)
Minecraft install resource pack
libusb的使用
Openshift build image
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
1、 QT's core class QObject
First week of JS study
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
Leetcode sword finger offer brush questions - day 22
Solution and analysis of Hanoi Tower problem
Gateway is easy to use




