登录
首页 >  Golang >  Go问答

使用 HTTP 请求将项目添加到已有列表

来源:stackoverflow

时间:2024-02-06 10:27:20 163浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《使用 HTTP 请求将项目添加到已有列表》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

我正在使用 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" 括在括号中)

今天关于《使用 HTTP 请求将项目添加到已有列表》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>