1. Iris起服务

package main

import "github.com/kataras/iris"

func main() {
//1.创建app结构体对象
app := iris.New() //返回一个application
//2.端口监听(启动服务本质就是监听端口)
//iris.WithoutServerError 设置错误
app.Run(iris.Addr(":7999"), iris.WithoutServerError(iris.ErrServerClosed))
//也可以不设置错误
//application.Run(iris.Addr(":8080"))
//application.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed)) //第二种 }

2. 数据请求方式的分类

所有的项目中使用的请求都遵循HTTP协议标准,HTTP协议经过了1.0和1.1两个版本的发展。

  • HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法。

  • HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。

因此,我们可以说,HTTP协议一共定义了八种方法用来对Request-URI网络资源的不同操作方式,这些操作具体为:GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT等八种操作方式。

请求方式:

package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
) func main() { app := iris.New()
//第一个参数是路径
//第二个是匿名函数,处理请求
app.Get("/getRequest", func(context context.Context) {
//处理get请求,请求的url为:/getRequest
path := context.Path()
//以日志的方式打印
app.Logger().Info(path) //2020/02/05 07:17 /getRequest
}) //1.处理Get请求
app.Get("/userpath", func(context context.Context) {
//获取Path
path := context.Path()
app.Logger().Info(path) //2020/02/05 07:18 /userpath
//写入返回数据:string类型
context.WriteString("请求路径:" + path)
}) //2.处理Get请求 并接受参数
//http://localhost:8000/userinfo?username=yang&pwd=123456
app.Get("/userinfo", func(context context.Context) {
path := context.Path()
app.Logger().Info(path)
//获取get请求所携带的参数
userName := context.URLParam("username")
app.Logger().Info(userName) pwd := context.URLParam("pwd")
app.Logger().Info(pwd)
//返回html数据格式
context.HTML("<h1>" + userName + "," + pwd + "</h1>")
}) //3.处理Post请求 form表单的字段获取
app.Post("/postLogin", func(context context.Context) {
path := context.Path()
app.Logger().Info(path)
//context.PostValue方法来获取post请求所提交的form表单数据
name := context.PostValue("name")
pwd := context.PostValue("pwd")
app.Logger().Info(name, " ", pwd)
context.HTML(name)
}) //4.处理Post请求 Json格式数据 //Postman工具选择[{"key":"Content-Type","value":"application/json","description":""}]
//请求内容:{"name": "davie","age": 28} app.Post("/postJson", func(context context.Context) { //1.path
path := context.Path()
app.Logger().Info("请求URL:", path) //2.Json数据解析
var person Person
//通过context.ReadJSON()读取传过来的数据
if err := context.ReadJSON(&person); err != nil {
panic(err.Error())
}
//返回格式化的内容
context.Writef("Received: %#+v\n", person)
}) //5.处理Post请求 Xml格式数据
/*
* 请求配置:Content-Type到application/xml(可选但最好设置)
* 请求内容:
*
* <student>
* <stu_name>davie</stu_name>
* <stu_age>28</stu_age>
* </student>
*
*/
app.Post("/postXml", func(context context.Context) { //1.Path
path := context.Path()
app.Logger().Info("请求URL:", path) //2.XML数据解析
var student Student
if err := context.ReadXML(&student); err != nil {
panic(err.Error())
}
//返回格式化的内容
context.Writef("Received:%#+v\n", student)
}) //6.put请求
app.Put("/putinfo", func(context context.Context) {
path := context.Path()
app.Logger().Info("请求url:", path)
}) //7.delete请求
app.Delete("/deleteuser", func(context context.Context) {
path := context.Path()
app.Logger().Info("Delete请求url:", path)
}) app.Run(iris.Addr(":8000"), iris.WithoutServerError(iris.ErrServerClosed))
} //自定义的struct
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
} //自定义的结构体
type Student struct {
//XMLName xml.Name `xml:"student"`
StuName string `xml:"stu_name"`
StuAge int `xml:"stu_age"`
} type config struct {
Addr string `yaml:"addr"`
ServerName string `yaml:"serverName"`
}

数据返回类型:

package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
) type Teacher struct {
Name string `json:"Name"`
City string `json:"City"`
Other string `json:"Other"`
} //程序主入口
func main() { app := iris.New() /**
* 通过Get请求
* 返回 WriteString
*/
app.Get("/getHello", func(context context.Context) {
context.WriteString(" Hello world ")
}) /**
* 通过Get请求
* 返回 HTML数据
*/
app.Get("/getHtml", func(context context.Context) {
context.HTML("<h1> Davie, 12 </h1>")
}) /**
* 通过Get请求
* 返回 Json数据
*/
app.Get("/getJson", func(context context.Context) {
context.JSON(iris.Map{"message": "hello word", "requestCode": 200})
}) //POST
app.Post("/user/postinfo", func(context context.Context) {
//context.WriteString(" Post Request ")
//user := context.Params().GetString("user") var tec Teacher
err := context.ReadJSON(&tec)
if err != nil {
panic(err.Error())
} else {
app.Logger().Info(tec)
context.WriteString(tec.Name)
} //fmt.Println(user)
//teacher := Teacher{}
//err := context.ReadForm(&teacher)
//if err != nil {
// panic(err.Error())
//} else {
// context.WriteString(teacher.Name)
//}
}) //PUT
app.Put("/getHello", func(context context.Context) {
context.WriteString(" Put Request ")
}) app.Delete("/DeleteHello", func(context context.Context) {
context.WriteString(" Delete Request ")
}) //返回json数据
app.Get("/getJson", func(context context.Context) {
context.JSON(iris.Map{"message": "hello word", "requestCode": 200})
}) app.Get("/getStuJson", func(context context.Context) {
context.JSON(Student{Name: "Davie", Age: 18})
}) //返回xml数据
app.Get("/getXml", func(context context.Context) {
context.XML(Person{Name: "Davie", Age: 18})
}) //返回Text
app.Get("/helloText", func(context context.Context) {
context.Text(" text hello world ")
}) app.Run(iris.Addr(":8000"), iris.WithoutServerError(iris.ErrServerClosed)) } //json结构体
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
} //xml结构体
type Person struct {
Name string `xml:"name"`
Age int `xml:"age"`
}

Iris请求方式和数据返回类型的更多相关文章

  1. Postman 的 Post 请求方式的四种类型的数据

    Postman 的 Post 请求方式的四种类型的数据 1. form-data 2. x-www-form-urlencoded 3. raw 4. binary 1. form-data 就是 H ...

  2. Nginx下HTML页面POST请求静态JSON数据返回405状态

    在浏览器访问HTML页面,发现一些静态JSON数据没有显示,F12查看,如下图所示: 可以看到请求方式为POST 将请求链接复制在浏览器地址栏访问,可以正常请求到数据 F12查看,可以看到请求方式为G ...

  3. jquery ajax请求成功,数据返回成功,seccess不执行的问题

    1.状态码返回200--表明服务器正常响应了客户端的请求:       2.通过firebug和IE的httpWatcher可以看出服务器端返回了正常的数据,并且是符合业务逻辑的数据.         ...

  4. Postman的Post请求方式的四种类型的数据

    1. form-data 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时 ...

  5. 前端使用node.js+express+mockjs+mysql实现简单服务端,2种方式模拟数据返回

    今天,我教大家来搭建一个简单服务端 参考文章: https://www.jianshu.com/p/cb89d9ac635e https://www.cnblogs.com/jj-notes/p/66 ...

  6. C# WebApi 请求方式Post,返回Response

    1.[FromBody]属性只能用在一个参数上,当Body中有多个参数要定义类型.一个参数的时候 key="",value="123",key为空才能取到值. ...

  7. 使用jquery通过AJAX请求方式,后台返回了当前整个HTML页面代码

    该结果分为多种情况: 1.当前项目使用了interceptor/filter,拦截或者过滤了特定请求. 2.在HTML页面使用了表单提交,没有对表单的“onsubmit”事件做return false ...

  8. rest接口webservice接口利用http请求方式发送数据

    所需依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>h ...

  9. GoWeb开发_Iris框架讲解(二):Get、Post、Put等请求及数据返回格式

    数据请求方式的分类 所有的项目中使用的请求都遵循HTTP协议标准,HTTP协议经过了1.0和1.1两个版本的发展. HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法. HTTP ...

随机推荐

  1. Tomcat目录详解

    最近在项目部署时,有时通过使用公司提供的Jdoc容器引擎部署上线项目,有时使用Jenkins自动化部署,甚至有的项目直接打war包上传到弹性云的tomcat中进行部署.虽然部署方式略有不同,但是归根结 ...

  2. 牛客寒假训练营2-H施魔法

    思路 dp去维护前缀f[i-1] - ai的最小值 CODE #include <bits/stdc++.h> #define dbg(x) cout << #x <&l ...

  3. javaScript中的异步编程模式

    1.事件模型 let button = document.getElementById("my-btn"); button.onclick = function(event) { ...

  4. weflow的坑

    开发工具weflow的坑 使用less,css预处理器.如果less文件有问题项目不能正常打开需要排除错误后才能正常打开.今天遇到个问题background:rgba(255,255,255 .35) ...

  5. 模拟退火SA刷题记录

    洛谷P1337 [JSOI2004]平衡点 / 吊打XXX 基本上是照着别人的代码写的,模拟退火为什么一定能找到答案呢...迷惑,,有时间搜一搜证明啥的 sa步骤:这个是要确定一个(xi,yi)使得函 ...

  6. 占位 DL

    占位 DL include: DL404

  7. jsp报错java.io.IOException: Stream closed

    在使用jsp的时候莫名其妙的抛出了这个异常,经过反复检查 去掉了网友们说的jsp使用流未关闭,以及tomcat版本冲突等原因,最后发现是书写格式的原因. 当时使用的代码如下 <jsp:inclu ...

  8. Jmeter-文件目录

    Jmeter文件目录介绍 1.bin:可执行文件目录 (1)jmeter.bat:windows的启动文件 (2)jmeter.log:日志文件 (3)jmeter.sh:linux的启动文件 (4) ...

  9. 添加Usb3.0驱动到win7/8/10的usb安装光盘

    文章中所有需要使用到的软件和资源在文章末尾的网盘连接中有下载 Run the DISM GUI Tool Right-click on DISM GUI.exe and select Run as A ...

  10. JS Data 时间对象

    new Date() 返回当前的本地日期和时间