首页 > 后端开发 > Golang > 正文

通过 http 请求追加到现有列表

WBOY
发布: 2024-02-14 15:57:07
转载
1027人浏览过

通过 http 请求追加到现有列表

php小编小新通过 http 请求追加到现有列表是一种常见的数据操作方式。通过发送 http 请求,我们可以将新的数据追加到已有的列表中,实现数据的动态更新和增加。这种方法在网页开发中十分常用,可以实现用户提交数据后的实时显示和更新。通过 http 请求追加到现有列表的操作简单快捷,可以提高网页的交互性和用户体验。无论是在前端页面还是后端逻辑中,都可以通过这种方式实现数据的追加,实现更加丰富和实用的功能。

问题内容

我正在使用 echo 制作一个简单的 rest api。我有一个变量,它是以下地图,基于我制作的这个结构:

type checklist struct {
        id         int      `json:"id"`
        title      string   `json:"title"`
        lines      []string `json:"lines"`
        authorname string   `json:"authorname"`
        authorid   int      `json:"authorid"`
        tags       []tag    `json:"tags"`
    }

    var (
        checklists    = map[int]*checklist{}
        checklistseq  = 1
        checklistlock = sync.mutex{}
    )
登录后复制

创建新清单并将其附加到 checklists 变量后,如何发送在新清单的“行”字段中附加新行的请求?

我想到的解决方案是这样的:

func createchecklist(c echo.context) error {
        checklistlock.lock()
        defer checklistlock.unlock()
        newchecklist := &checklist{
            id:    checklistseq,
            lines: make([]string, 0),
            tags:  make([]tag, 0),
        }
        if err := c.bind(newchecklist); err != nil {
            return err
        }
        checklists[newchecklist.id] = newchecklist
        checklistseq++
        return c.json(http.statusok, newchecklist)
    }
    func addline(c echo.context) error {
        checklistlock.lock()
        defer checklistlock.unlock()
        id, _ := strconv.atoi(c.param("id"))
        checklist := *checklists[id]
        line := []string{""}
        if err := c.bind(line); err != nil {
            return err
        }
        checklist.lines = line
        return c.json(http.statuscreated, checklists)
    }
登录后复制

但是,当我测试此处理程序时,它给出了以下结果:

与光AI
与光AI

一站式AI视频工作流创作平台

与光AI 66
查看详情 与光AI
// 1: Creating a new checklist

    $ curl -X POST -H 'Content-Type: application/json' -d '{"title": "test"}' localhost:1234/checklist

    >> {"id":1,"title":"test","lines":[],"authorName":"","authorID":0,"tags":[]}

    // 2: Check to see the checklist has been created.
 
    $ curl -X GET localhost:1234/checklist

    >> {"1":{"id":1,"title":"test","lines":[],"authorName":"","authorID":0,"tags":[]}}
    // 3: Attempting to create a new line
    $ curl -X POST -H 'Content-Type: application/json' -d '{"lines": "test123"}' localhost:1234/checklist/1

    >> curl: (52) Empty reply from server

    // 4: Confirming it hasn't been created.

    $ curl -X GET localhost:1234/checklist

    >> {"1":{"id":1,"title":"test","lines":[],"authorName":"","authorID":0,"tags":[]}}
登录后复制

因此该函数实际上不起作用,因为将 post ping 到适当的路由时既没有返回预期的响应,也没有将该行实际添加到字段中。

解决方法

func addline(c echo.context) error {
    checklistlock.lock()
    defer checklistlock.unlock()

    id, err := strconv.atoi(c.param("id"))
    if err != nil {
        return err
    }

    // do not deref *checklist
    cl, ok := checklists[id]
    if !ok {
        return echo.errnotfound
    }

    // use same structure as the expected json
    var input struct { lines []string `json:"lines"` }

    // always pass a pointer to c.bind
    if err := c.bind(&input); err != nil {
        return err
    }

    // do not overwrite previously written lines, use append
    cl.lines = append(cl.lines, input.lines...)

    return c.json(http.statusok, cl)
}
登录后复制

现在尝试:

$ curl -X POST -H 'Content-Type: application/json' -d '{"lines": ["test123"]}' localhost:1234/checklist/1
登录后复制

(注意 "test123" 括在括号中)

以上就是通过 http 请求追加到现有列表的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号