如何通过Postman在go lang中处理GET操作(CRUD)?
How to process GET operation (CRUD) in go lang via Postman?
我想执行获取操作。我将名称作为资源传递给 URL。
我在 Postman 中打的 URL 是:localhost:8080/location/{titan rolex}
(我在下拉列表中选择了 GET 方法)
在 Postman 中的 URL 命中,我正在执行 GetUser func() with body as:
func GetUser(rw http.ResponseWriter, req *http.Request) {
}
现在我希望在 GetUser 方法中获取资源值,即 'titan rolex'。
我怎样才能在 golang 中实现这一点?
在 main() 中,我有这个:
http.HandleFunc("/location/{titan rolex}", GetUser)
提前致谢。
您正在做的是绑定完整路径/location/{titan rolex}
,由GetUser
处理。
您真正想要的是绑定 /location/<every possible string>
由一个处理程序处理(例如 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/<every possible string>/<some int>/<another string>
)将通过这种方式实现起来很乏味。
另一种方法是使用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
- 如果其他东西足够好,它将 URLEncode 为 http://localhost:8080/location/titan%20rolex
。
我想执行获取操作。我将名称作为资源传递给 URL。
我在 Postman 中打的 URL 是:localhost:8080/location/{titan rolex}
(我在下拉列表中选择了 GET 方法)
在 Postman 中的 URL 命中,我正在执行 GetUser func() with body as:
func GetUser(rw http.ResponseWriter, req *http.Request) {
}
现在我希望在 GetUser 方法中获取资源值,即 'titan rolex'。 我怎样才能在 golang 中实现这一点?
在 main() 中,我有这个:
http.HandleFunc("/location/{titan rolex}", GetUser)
提前致谢。
您正在做的是绑定完整路径/location/{titan rolex}
,由GetUser
处理。
您真正想要的是绑定 /location/<every possible string>
由一个处理程序处理(例如 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/<every possible string>/<some int>/<another string>
)将通过这种方式实现起来很乏味。另一种方法是使用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
- 如果其他东西足够好,它将 URLEncode 为 http://localhost:8080/location/titan%20rolex
。