使用不同的查询参数,将 POST 添加到 GET 相同的端点,导致错误消息的不一致性
来源:stackoverflow
2024-02-06 13:18:23
0浏览
收藏
大家好,今天本人给大家带来文章《使用不同的查询参数,将 POST 添加到 GET 相同的端点,导致错误消息的不一致性》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容
当使用不同的方法添加相同的路由时,每个方法查询 get 调用的响应是不同的,但是由于另一个方法是 post,所以它应该不受影响。
通过帖子: 游乐场:https://go.dev/play/p/xzoakpehggy
// you can edit this code!
// click here and start typing.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.newrouter()
r.handlefunc("/api/v2", func(w http.responsewriter, r *http.request) {
// an example api handler
fmt.fprintf(w, "you made a post request")
json.newencoder(w).encode(map[string]bool{"ok": true})
}).methods("post")
r.handlefunc("/api/v2", func(w http.responsewriter, r *http.request) {
// an example api handler
fmt.fprintf(w, "you made a get request")
json.newencoder(w).encode(map[string]bool{"ok": true})
}).
queries("from", "{from:[0-9]+}",
"to", "{to:[0-9]+}").methods("get")
srv := &http.server{
handler: r,
addr: "127.0.0.1:8000",
// good practice: enforce timeouts for servers you create!
writetimeout: 15 * time.second,
readtimeout: 15 * time.second,
}
go srv.listenandserve()
req2 := httptest.newrequest("get", "/api/v2?from=3&to=-5", nil)
out2 := httptest.newrecorder()
r.servehttp(out2, req2)
fmt.println(out2.code)
fmt.println(out2)
}
预期为 404,实际为 405
删除 post 时 游乐场:https://go.dev/play/p/exif00_wrfw
// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a GET request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).
Queries("from", "{from:[0-9]+}",
"to", "{to:[0-9]+}").Methods("GET")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
go srv.ListenAndServe()
req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
out2 := httptest.NewRecorder()
r.ServeHTTP(out2, req2)
fmt.Println(out2.Code)
}
结果为 404
对于 get 请求,路由和结果应该一致。 404-s
我很好奇以前是否有人遇到过这个问题?
正确答案
路由器将尝试根据路径和查询参数查找匹配项。由于查询字符串参数不符合要求,导致 get 路由未匹配。
但是该路径与 post 路由匹配,因为该路由不关心这些查询参数。然后检查请求方法,它与路由不匹配,因此返回 405 method not allowed (该路由有一个处理程序,但方法不同)。
您可以通过为同一路径添加包罗万象的 get 处理程序来获得所需的行为:
// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a POST request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).Methods("POST")
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a GET request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).
Queries("from", "{from:[0-9]+}",
"to", "{to:[0-9]+}").
Methods("GET")
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusNotFound)
}).Methods("GET")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
go srv.ListenAndServe()
req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
out2 := httptest.NewRecorder()
r.ServeHTTP(out2, req2)
fmt.Println(out2.Code)
}
本篇关于《使用不同的查询参数,将 POST 添加到 GET 相同的端点,导致错误消息的不一致性》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
无法通过lua验证令牌
- 上一篇
- 无法通过lua验证令牌
- 下一篇
- 星期五九月:MIME 标头缺少冒号的格式错误
查看更多
最新文章
-
- Golang · Go问答 | 2小时前 |
- embed文件在测试包中找不到怎么办
- 313浏览 收藏
-
- Golang · Go问答 | 2小时前 | base64 Go RawURLEncoding
- base64RawURL结果包含非法字符怎么办
- 322浏览 收藏
-
- Golang · Go问答 | 2小时前 | 随机数 · go ·
- crypto/rand读取失败如何处理
- 105浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- NullString序列化成空字符串怎么办
- 421浏览 收藏
-
- Golang · Go问答 | 2小时前 | 事务 · go · 回滚 · database/sql ·
- 事务提交失败后如何保证回滚
- 478浏览 收藏
-
- Golang · Go问答 | 2小时前 | Go 连接池 database/sql sql.DB
- sql.DB连接池空闲连接怎么调
- 177浏览 收藏
-
- Golang · Go问答 | 2小时前 | go · 连接池 · database/sql ·
- sqlRows没有关闭导致连接池堵塞怎么办
- 243浏览 收藏
-
- Golang · Go问答 | 2小时前 | xml · 反序列化 · Go问答 · Go encoding/xml XML命名空间 XML前缀
- XML命名空间前缀读取为空怎么办
- 283浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- CSV字段中换行导致读取错位怎么办
- 409浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 1036次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 1034次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 1178次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 1139次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 981次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 正则命名捕获组取不到值怎么办
- 2026-11-15 501浏览
-
- Go pprof profile duration 参数如何理解
- 2026-11-14 501浏览
-
- Go types.Selection.Type 与 Obj.Type 为什么不总是相同
- 2026-11-10 501浏览
