透過 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"
括在括號中)
以上是透過 http 請求追加到現有列表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...
