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

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

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)。

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

  1. 标准库:
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/<任意字符串>/<某个整数>/<另一个字符串>)会很繁琐。

  1. 另一种方法是使用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/&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:

     import (
         &quot;fmt&quot;
         &quot;net/http&quot;
         &quot;log&quot;
     )
    
     func locationHandler(w http.ResponseWriter, r *http.Request) {
         name := r.URL.Path[len(&quot;/location/&quot;):]
         fmt.Fprintf(w, &quot;Location: %s\n&quot;, name)
     }
    
     func main() {
         http.HandleFunc(&quot;/location/&quot;, locationHandler)
         log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
     }
    

    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:

     import (
         &quot;fmt&quot;
         &quot;github.com/julienschmidt/httprouter&quot;
         &quot;net/http&quot;
         &quot;log&quot;
     )
    
     func LocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
         fmt.Fprintf(w, &quot;Location: %s\n&quot;, ps.ByName(&quot;loc&quot;))
     }
    
     func main() {
         router := httprouter.New()
         router.GET(&quot;/location/:loc&quot;, LocationHandler)
    
         log.Fatal(http.ListenAndServe(&quot;:8080&quot;, 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.

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:

确定