卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35668本站已运行398

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

通过 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)
    }

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

// 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" 括在括号中)

卓越飞翔博客
上一篇: 使用 Content-Type multipart/form-data 发布数据
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏