英文:
How to process GET operation (CRUD) in go lang via Postman?
问题
我想执行一个GET操作。我将名称作为资源传递给URL。
我在Postman中访问的URL是:localhost:8080/location/{titan rolex}
(我在下拉列表中选择了GET方法)
在Postman中访问URL时,我使用以下代码执行GetUser函数:
func GetUser(rw http.ResponseWriter, req *http.Request) {
}
现在,我希望在GetUser方法中获取资源值,即'titan rolex'。
在golang中,我该如何实现这一点?
在main()函数中,我有以下代码:
http.HandleFunc("/location/{titan rolex}", GetUser)
提前感谢。
英文:
I want to perform a get operation. I am passng name as a resource to the URL.
The URL I am hitting in Postman is : localhost:8080/location/{titan rolex}
( I chose the GET method in the dropdown list)
On the URL hit in Postman, I am executing the GetUser func() with body as:
func GetUser(rw http.ResponseWriter, req *http.Request) {
}
Now I wish to get the resource value i.e 'titan rolex' in the GetUser method.
How can I achieve this in golang?
In main(), I have this :
http.HandleFunc("/location/{titan rolex}", GetUser)
Thanks in advance.
答案1
得分: 0
你正在做的是将路径/location/{titan rolex}
绑定到GetUser
处理程序。
你真正想要的是将/location/<任意字符串>
绑定到一个处理程序(例如LocationHandler
)。
你可以使用标准库或另一个路由器来实现。我将介绍两种方法:
- 标准库:
import (
"fmt"
"net/http"
"log"
)
func locationHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[len("/location/"):]
fmt.Fprintf(w, "Location: %s\n", name)
}
func main() {
http.HandleFunc("/location/", locationHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
然而,请注意,使用这种方式实现更复杂的路径(例如/location/<任意字符串>/<某个整数>/<另一个字符串>
)会很繁琐。
- 另一种方法是使用github.com/julienschmidt/httprouter,特别是当你经常遇到这些情况(并且有更复杂的路径)时。
以下是你的用例示例:
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
func LocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "Location: %s\n", ps.ByName("loc"))
}
func main() {
router := httprouter.New()
router.GET("/location/:loc", LocationHandler)
log.Fatal(http.ListenAndServe(":8080", router))
}
请注意,httprouter
对处理程序的签名略有不同。这是因为,正如你所看到的,它也将这些参数传递给函数。
另外,请注意,你可以使用浏览器(或其他工具)访问http://localhost:8080/location/titan rolex
,如果该工具足够好,它会将其URL编码为http://localhost:8080/location/titan%20rolex
。
英文:
What you are doing is binding the complete path /location/{titan rolex}
to be handled by GetUser
.
What you really want is to bind /location/<every possible string>
to be handled by one handler (e.g. LocationHandler
).
You can do that with either the standard library or another router. I will present both ways:
-
Standard lib:
import ( "fmt" "net/http" "log" ) func locationHandler(w http.ResponseWriter, r *http.Request) { name := r.URL.Path[len("/location/"):] fmt.Fprintf(w, "Location: %s\n", name) } func main() { http.HandleFunc("/location/", locationHandler) log.Fatal(http.ListenAndServe(":8080", nil)) }
Note however, more complex paths (such as
/location/<every possible string>/<some int>/<another string>
) will be tedious to implement this way. -
The other way is to use github.com/julienschmidt/httprouter, especially if you encounter these situations more often (and have more complex paths).
Here's an example for your use case:
import ( "fmt" "github.com/julienschmidt/httprouter" "net/http" "log" ) func LocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { fmt.Fprintf(w, "Location: %s\n", ps.ByName("loc")) } func main() { router := httprouter.New() router.GET("/location/:loc", LocationHandler) log.Fatal(http.ListenAndServe(":8080", router)) }
Note that
httprouter
uses a slightly different signature for handlers. This is because, as you can see, it passes these parameters to the functions as well.
Oh and another note, you can just hit http://localhost:8080/location/titan rolex
with your browser (or something else) - if that something else is decent enough, it will URLEncode that to be http://localhost:8080/location/titan%20rolex
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论