请求数据格式具体代码

未匹配的标注

返回json html text xml等不同形式

package main

import (
    "github.com/kataras/iris"
    "github.com/kataras/iris/context"
)

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("<h3> huxiaobai , 30</h3>")
    })
    /*
     *通过get请求 返回json数据
     */
    app.Get("/getJson", func(context context.Context) {
        user := User1{Username:"huxiaobai", Pass:"dfjskjsd"}
        user1 := map[string]string{"username": "hushaoliang", "password": "wahahaha"}
        context.JSON(user)  //可以对结构体进行json转化
        context.JSON(user1) //也可以对map字典进行json转化
        context.JSON(iris.Map{"username":"huxiaobai","password":"wahahaha"}) //还可以这么将map字典转化为json
    })

    /*
     *通过get请求 返回text文本格式
     */
    app.Get("/getText", func(context context.Context) {
        context.Text("wo shi hushaoliang")
    })


    app.Run(iris.Addr(":8010"),iris.WithoutServerError(iris.ErrServerClosed))
}

type User1 struct {
    Username string `json:"username"`
    Pass string `json:"pass"`
}

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~