如何在 GoLang 中使用 http POST 方法来执行记录更新操作?
来源:stackoverflow
时间:2024-02-19 20:12:26 450浏览 收藏
学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《如何在 GoLang 中使用 http POST 方法来执行记录更新操作?》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!
问题描述:
我正在学习 golang 来为一个小项目实现 rest api。 我正在遵循这个小例子来了解如何连接事物。 但是,示例示例中似乎存在一些错误,导致我在到达端点后无法在邮递员中获得预期响应。 我通过添加缺少的函数(handlefunc 函数)来修复它以使其正常工作。
问题描述:
但是,我对 createevent 部分仍然有疑问。 预期是在使用 post 方法和给定的示例事件(json 格式)(如下所示)后,事件列表会更新。
{ "id": "23", "title": "this is simple go lang title for test!", "description":"in this course you will learn rest api implementation in go lang" }
但是在到达我定义的返回所有事件的“http://localhost:8080/events”端点(1 个在代码内部定义,另一个应该通过调用 createevent 函数添加)之后,我只得到其中一个事件(仅硬编码内部代码)作为响应。
这是完整的代码。 感谢您的任何建议/评论。
package main import ( "fmt" "log" "net/http" "io/ioutil" "encoding/json" "github.com/gorilla/mux" ) func homeLink(w http.ResponseWriter, r *http.Request) { fmt.Println("test started!") fmt.Fprintf(w, "Welcome home!") } func main() { router := mux.NewRouter().StrictSlash(true) router.HandleFunc("/", homeLink) /*i have added the next 3 lines, missing in the sample code*/ router.HandleFunc("/event", createEvent) router.HandleFunc("/events/{id}", getOneEvent) router.HandleFunc("/events", getAllEvents) log.Fatal(http.ListenAndServe(":8080", router)) } type event struct { ID string `json:"ID"` Title string `json:"Title"` Description string `json:"Description"` } type allEvents []event var events = allEvents{ { ID: "1", Title: "Introduction to Golang", Description: "Come join us for a chance to learn how golang works and get to eventually try it out", }, } func createEvent(w http.ResponseWriter, r *http.Request) { var newEvent event reqBody, err := ioutil.ReadAll(r.Body) if err != nil { fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update") } fmt.Println("Create Event is called!") json.Unmarshal(reqBody, &newEvent) events = append(events, newEvent) w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(newEvent) } func getOneEvent(w http.ResponseWriter, r *http.Request) { eventID := mux.Vars(r)["id"] fmt.Println("get one event is called!") fmt.Println(eventID) for _, singleEvent := range events { if singleEvent.ID == eventID { json.NewEncoder(w).Encode(singleEvent) } } } func getAllEvents(w http.ResponseWriter, r *http.Request) { fmt.Println("Get all events is called!") json.NewEncoder(w).Encode(events) }
正确答案
您的代码运行良好。我已经测试过它(只需复制上面的代码并在我的本地计算机中运行并使用 postman 进行测试)。
顺便说一句,我在下面添加了一些关于更好代码的建议。
如果不存在nil错误,则处理并返回。
reqbody, err := ioutil.readall(r.body) if err != nil { fmt.fprintf(w, "kindly enter data with the event title and description only in order to update") return //add this return, otherwise continue the function with the error }
将此 json 错误处理放入 createevent handler 函数
err = json.unmarshal(reqbody, &newevent) if err != nil { fmt.fprintf(w, "json format invalid") return }
将 http 方法添加到您的端点。
195657188139以上就是《如何在 GoLang 中使用 http POST 方法来执行记录更新操作?》的详细内容,更多关于的资料请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习