码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 从0开始学go第七天


    gin获取表单from中的数据

    模拟简单登录页面:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>logintitle>
    6. head>
    7. <body>
    8. <form action="/login" method="post">
    9. <div>
    10. <label for="username">username:label>
    11. <input type="text" name="username" id="username">
    12. div>
    13. <div>
    14. <label for="password">password:label>
    15. <input type="text" name="password" id="password">
    16. div>
    17. <div>
    18. //当submit被点击以后,会向服务端发送请求,发送方式为post
    19. <input type="submit" value="登录">
    20. div>
    21. form>
    22. body>
    23. html>

    对于POST的返回页面:

    1. "en">
    2. "UTF-8">
    3. index
    4. Hello , {{ .Name }}!

    5. 你的密码是 : {{ .Password }}

    获取方法一代码:

    1. package main
    2. //from表单提交的参数
    3. import (
    4. "net/http"
    5. "github.com/gin-gonic/gin"
    6. )
    7. func main() {
    8. r := gin.Default()
    9. r.LoadHTMLFiles("./login.html", "./index.html")
    10. r.GET("/login", func(c *gin.Context) {
    11. c.HTML(http.StatusOK, "login.html", nil)
    12. })
    13. //POST请求
    14. r.POST("/login", func(c *gin.Context) {
    15. username := c.PostForm("username")
    16. password := c.PostForm("password")
    17. c.HTML(http.StatusOK, "index.html", gin.H{
    18. "Name": username,
    19. "Password": password,
    20. })
    21. })
    22. r.Run(":9090")
    23. }

    获取方法二代码:

    带默认值

    1. package main
    2. //from表单提交的参数
    3. import (
    4. "net/http"
    5. "github.com/gin-gonic/gin"
    6. )
    7. func main() {
    8. r := gin.Default()
    9. r.LoadHTMLFiles("./login.html", "./index.html")
    10. r.GET("/login", func(c *gin.Context) {
    11. c.HTML(http.StatusOK, "login.html", nil)
    12. })
    13. //POST请求
    14. r.POST("/login", func(c *gin.Context) {
    15. // username := c.PostForm("username")
    16. // password := c.PostForm("password")
    17. username := c.DefaultPostForm("username", "somebody")
    18. password := c.DefaultPostForm("password", "*******")
    19. c.HTML(http.StatusOK, "index.html", gin.H{
    20. "Name": username,
    21. "Password": password,
    22. })
    23. })
    24. r.Run(":9090")
    25. }

    方法三:GetPostFrom

    1. package main
    2. //from表单提交的参数
    3. import (
    4. "net/http"
    5. "github.com/gin-gonic/gin"
    6. )
    7. func main() {
    8. r := gin.Default()
    9. r.LoadHTMLFiles("./login.html", "./index.html")
    10. r.GET("/login", func(c *gin.Context) {
    11. c.HTML(http.StatusOK, "login.html", nil)
    12. })
    13. //POST请求
    14. r.POST("/login", func(c *gin.Context) {
    15. // username := c.PostForm("username")
    16. // password := c.PostForm("password")
    17. //username := c.DefaultPostForm("username", "somebody")
    18. //password := c.DefaultPostForm("password", "*******")
    19. username,ok := c.GetPostForm("username")
    20. if !ok {
    21. username = "sb"
    22. }
    23. password,ok := c.GetPostForm("password")
    24. if !ok {
    25. password = "******"
    26. }
    27. c.HTML(http.StatusOK, "index.html", gin.H{
    28. "Name": username,
    29. "Password": password,
    30. })
    31. })
    32. r.Run(":9090")
    33. }

  • 相关阅读:
    代码随想录刷题|LeetCode 1143.最长公共子序列 1035.不相交的线 53. 最大子序和 动态规划
    Real closed field
    Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程
    文件分片上传和断点续传
    【CVPR2023】DetCLIPv2:通过单词-区域对齐实现可扩展的开放词汇目标检测预训练...
    muduo源码学习base——Atomic(原子操作与原子整数)
    浏览器:重绘(repaint)与回流/重排(reflow)
    微服务面试问题小结( 微服务、分布式、MQ、网关、zookeeper、nginx)
    芯片设计“花招”已耍完?无指令集架构颠覆旧套路
    chrome浏览器查看css样式
  • 原文地址:https://blog.csdn.net/qq_59183443/article/details/133780287
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号