将参数传递给GET请求

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

Passing parameters to GET request

问题

我似乎无法弄清楚如何在go中完成这个任务。

我正在按照这个教程进行操作:

https://github.com/parnurzeal/gorequest

我可以使用Header传递参数,但我认为这是一个不好的做法。

我基本上在寻找go版本的python代码。

以下是我REST API的代码:

  1. package main
  2. import (
  3. "net/http"
  4. "strconv"
  5. "fmt"
  6. )
  7. func make_result(w http.ResponseWriter, r *http.Request) {
  8. fmt.Println(r)
  9. err := r.ParseForm()
  10. if err != nil {
  11. panic(err)
  12. }
  13. number_string := r.Form["number"][0]
  14. // number_string := r.Header["Number"][0] // 使用Header的解决方案
  15. number, err := strconv.Atoi(number_string)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Fprint(w, fmt.Sprint(number * 5))
  20. }
  21. func main() {
  22. http.HandleFunc("/get_result", make_result)
  23. http.ListenAndServe("localhost:8000", nil)
  24. }

我尝试使用以下代码调用它:

  1. package main
  2. import(
  3. "fmt"
  4. "reflect"
  5. "github.com/parnurzeal/gorequest"
  6. )
  7. func main() {
  8. resp, body, errs := gorequest.New().
  9. Get("http://localhost:8000/get_result").
  10. Set("Number", "7"). // 修改Header
  11. Type("form"). // 这两行似乎无关紧要
  12. Send(`{"number": 5}`). // 这两行似乎无关紧要
  13. End()
  14. fmt.Println(errs)
  15. fmt.Println(resp)
  16. fmt.Println(body)
  17. }

上述代码类似于Python的代码:

  1. import requests
  2. r = requests.get("http://localhost:8000/get_result", headers={"Number": 7})

当我使用Python的方法(使用params)调用API时,我看到/get_result?number=7 <nil> <nil>这一行作为request object的一部分被打印出来。但是在我的go版本中没有看到它,所以我一定是调用方法错误了。我漏掉了什么?

英文:

I cannot seem to figure how to do this in go.

I was following this tutorial:

https://github.com/parnurzeal/gorequest

and I can pass parameters using Header, which I think is a bad idea.

I am basically looking for go version of python

  1. In [28]: import requests
  2. In [29]: r = requests.get(&quot;http://localhost:8000/get_result&quot;, params={&quot;number&quot;: 40})

Following is my code for my REST API:

  1. package main
  2. import (
  3. &quot;net/http&quot;
  4. &quot;strconv&quot;
  5. &quot;fmt&quot;
  6. )
  7. func make_result(w http.ResponseWriter, r *http.Request) {
  8. fmt.Println(r)
  9. err := r.ParseForm()
  10. if err != nil {
  11. panic(err)
  12. }
  13. number_string := r.Form[&quot;number&quot;][0]
  14. // number_string := r.Header[&quot;Number&quot;][0] header solution
  15. number, err := strconv.Atoi(number_string)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Fprint(w, fmt.Sprint(number * 5))
  20. }
  21. func main() {
  22. http.HandleFunc(&quot;/get_result&quot;, make_result)
  23. http.ListenAndServe(&quot;localhost:8000&quot;, nil)
  24. }

I am trying to call it using this code:

  1. package main
  2. import(
  3. &quot;fmt&quot;
  4. &quot;reflect&quot;
  5. &quot;github.com/parnurzeal/gorequest&quot;
  6. )
  7. func main() {
  8. resp, body, errs := gorequest.New().
  9. Get(&quot;http://localhost:8000/get_result&quot;).
  10. Set(&quot;Number&quot;, &quot;7&quot;). // Changes the Header
  11. Type(&quot;form&quot;). // These two lines appear
  12. Send(`{&quot;number&quot;: 5}`). // to be irrelevant
  13. End()
  14. fmt.Println(errs)
  15. fmt.Println(resp)
  16. fmt.Println(body)
  17. }

The above is similar to python's:

  1. In [34]: r = requests.get(&quot;http://localhost:8000/get_result&quot;, headers={&quot;Number&quot;: 7})

When I am using the python method (using params) to call the api, I see /get_result?number=7 &lt;nil&gt; &lt;nil&gt; line being printed as a part of request object. But am don't see it in my go version, so I must be calling it wrong. What am I missing?

答案1

得分: 6

看起来你需要使用Param来实现这个功能。

此外,标准库的NewRequest返回一个带有URL成员的Request结构体,该成员具有一个Query函数,你可以在发出请求之前使用它来向查询中添加参数。

英文:

Looks like you need to use Param to do this.

Also the standard library's NewRequest returns a Request struct with a member URL that has a function Query that you can use to Add parameters to your query before issuing the request.

huangapple
  • 本文由 发表于 2015年11月2日 10:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/33469917.html
匿名

发表评论

匿名网友

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

确定