英文:
Passing parameters to GET request
问题
我似乎无法弄清楚如何在go
中完成这个任务。
我正在按照这个教程进行操作:
https://github.com/parnurzeal/gorequest
我可以使用Header
传递参数,但我认为这是一个不好的做法。
我基本上在寻找go
版本的python
代码。
以下是我REST API的代码:
package main
import (
"net/http"
"strconv"
"fmt"
)
func make_result(w http.ResponseWriter, r *http.Request) {
fmt.Println(r)
err := r.ParseForm()
if err != nil {
panic(err)
}
number_string := r.Form["number"][0]
// number_string := r.Header["Number"][0] // 使用Header的解决方案
number, err := strconv.Atoi(number_string)
if err != nil {
panic(err)
}
fmt.Fprint(w, fmt.Sprint(number * 5))
}
func main() {
http.HandleFunc("/get_result", make_result)
http.ListenAndServe("localhost:8000", nil)
}
我尝试使用以下代码调用它:
package main
import(
"fmt"
"reflect"
"github.com/parnurzeal/gorequest"
)
func main() {
resp, body, errs := gorequest.New().
Get("http://localhost:8000/get_result").
Set("Number", "7"). // 修改Header
Type("form"). // 这两行似乎无关紧要
Send(`{"number": 5}`). // 这两行似乎无关紧要
End()
fmt.Println(errs)
fmt.Println(resp)
fmt.Println(body)
}
上述代码类似于Python的代码:
import requests
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
In [28]: import requests
In [29]: r = requests.get("http://localhost:8000/get_result", params={"number": 40})
Following is my code for my REST API:
package main
import (
"net/http"
"strconv"
"fmt"
)
func make_result(w http.ResponseWriter, r *http.Request) {
fmt.Println(r)
err := r.ParseForm()
if err != nil {
panic(err)
}
number_string := r.Form["number"][0]
// number_string := r.Header["Number"][0] header solution
number, err := strconv.Atoi(number_string)
if err != nil {
panic(err)
}
fmt.Fprint(w, fmt.Sprint(number * 5))
}
func main() {
http.HandleFunc("/get_result", make_result)
http.ListenAndServe("localhost:8000", nil)
}
I am trying to call it using this code:
package main
import(
"fmt"
"reflect"
"github.com/parnurzeal/gorequest"
)
func main() {
resp, body, errs := gorequest.New().
Get("http://localhost:8000/get_result").
Set("Number", "7"). // Changes the Header
Type("form"). // These two lines appear
Send(`{"number": 5}`). // to be irrelevant
End()
fmt.Println(errs)
fmt.Println(resp)
fmt.Println(body)
}
The above is similar to python's:
In [34]: r = requests.get("http://localhost:8000/get_result", headers={"Number": 7})
When I am using the python method (using params) to call the api, I see /get_result?number=7 <nil> <nil>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论