如何通过Postman在Go语言中处理GET操作(CRUD)?

huangapple go评论127阅读模式
英文:

How to process GET operation (CRUD) in go lang via Postman?

问题

我想执行一个GET操作。我将名称作为资源传递给URL。
我在Postman中访问的URL是:localhost:8080/location/{titan rolex}(我在下拉列表中选择了GET方法)
在Postman中访问URL时,我使用以下代码执行GetUser函数:

  1. func GetUser(rw http.ResponseWriter, req *http.Request) {
  2. }

现在,我希望在GetUser方法中获取资源值,即'titan rolex'。
在golang中,我该如何实现这一点?

在main()函数中,我有以下代码:

  1. 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:

  1. func GetUser(rw http.ResponseWriter, req *http.Request) {
  2. }

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 :

  1. http.HandleFunc("/location/{titan rolex}", GetUser)

Thanks in advance.

答案1

得分: 0

你正在做的是将路径/location/{titan rolex}绑定到GetUser处理程序。

你真正想要的是将/location/<任意字符串>绑定到一个处理程序(例如LocationHandler)。

你可以使用标准库或另一个路由器来实现。我将介绍两种方法:

  1. 标准库:
  1. import (
  2. "fmt"
  3. "net/http"
  4. "log"
  5. )
  6. func locationHandler(w http.ResponseWriter, r *http.Request) {
  7. name := r.URL.Path[len("/location/"):]
  8. fmt.Fprintf(w, "Location: %s\n", name)
  9. }
  10. func main() {
  11. http.HandleFunc("/location/", locationHandler)
  12. log.Fatal(http.ListenAndServe(":8080", nil))
  13. }

然而,请注意,使用这种方式实现更复杂的路径(例如/location/<任意字符串>/<某个整数>/<另一个字符串>)会很繁琐。

  1. 另一种方法是使用github.com/julienschmidt/httprouter,特别是当你经常遇到这些情况(并且有更复杂的路径)时。

以下是你的用例示例:

  1. import (
  2. "fmt"
  3. "github.com/julienschmidt/httprouter"
  4. "net/http"
  5. "log"
  6. )
  7. func LocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  8. fmt.Fprintf(w, "Location: %s\n", ps.ByName("loc"))
  9. }
  10. func main() {
  11. router := httprouter.New()
  12. router.GET("/location/:loc", LocationHandler)
  13. log.Fatal(http.ListenAndServe(":8080", router))
  14. }

请注意,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/&lt;every possible string&gt; 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:

  1. Standard lib:

    1. import (
    2. &quot;fmt&quot;
    3. &quot;net/http&quot;
    4. &quot;log&quot;
    5. )
    6. func locationHandler(w http.ResponseWriter, r *http.Request) {
    7. name := r.URL.Path[len(&quot;/location/&quot;):]
    8. fmt.Fprintf(w, &quot;Location: %s\n&quot;, name)
    9. }
    10. func main() {
    11. http.HandleFunc(&quot;/location/&quot;, locationHandler)
    12. log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
    13. }

    Note however, more complex paths (such as /location/&lt;every possible string&gt;/&lt;some int&gt;/&lt;another string&gt;) will be tedious to implement this way.

  2. 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:

    1. import (
    2. &quot;fmt&quot;
    3. &quot;github.com/julienschmidt/httprouter&quot;
    4. &quot;net/http&quot;
    5. &quot;log&quot;
    6. )
    7. func LocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    8. fmt.Fprintf(w, &quot;Location: %s\n&quot;, ps.ByName(&quot;loc&quot;))
    9. }
    10. func main() {
    11. router := httprouter.New()
    12. router.GET(&quot;/location/:loc&quot;, LocationHandler)
    13. log.Fatal(http.ListenAndServe(&quot;:8080&quot;, router))
    14. }

    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.

huangapple
  • 本文由 发表于 2015年10月21日 16:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/33254561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定