gpt4 book ai didi

go - 如何在 go-wrk 命令中发送 post 数据值?

转载 作者:数据小太阳 更新时间:2023-10-29 03:15:04 27 4
gpt4 key购买 nike

在 curl 中,我可以通过 -d 标志发送发布数据,如下例

curl -X POST -d'{"accountID":"1"}' localhost:1234/geInfo

我应该如何在 go-wrk 中发送 accountID 值?发布请求的命令?

最佳答案

除非我弄错了,否则(目前)不支持传递 post 参数。

我从 go-wrk 的代码中通过遵循 -m="POST"参数得出了这一点,这表明情况并非如此。 (提供“POST”方法当然不代表你也可以传参)

参数在main.go中解析:19:

method            = flag.String("m", "GET", "the http request method")

然后在 single_node.go 中传递给客户:16:

go StartClient(
toCall,
*headers,
*method,
*disableKeepAlives,
responseChannel,
wg,
*totalCalls,
)

它在“meth”变量中排在第三位(client.go :14):

func StartClient(url_, heads, meth string, dka bool, responseChan chan *Response, waitGroup *sync.WaitGroup, tc int) {

然后在这里使用(client.go :55):

req, _ := http.NewRequest(meth, url_, nil)
sets := strings.Split(heads, "\n")

//Split incoming header string by \n and build header pairs
for i := range sets {
split := strings.SplitN(sets[i], ":", 2)
if len(split) == 2 {
req.Header.Set(split[0], split[1])
}
}

timer := NewTimer()
for {
timer.Reset()

resp, err := tr.RoundTrip(req)

respObj := &Response{}

(...)

responseChan <- respObj
}

如果 post 参数是可传递的,它们必须放在请求中的某个地方,因为您可以在 golang http package 上查找网站:

func NewRequest(method, urlStr string, body io.Reader) (*Request, error)

NewRequest 返回一个给定方法、URL 和可选正文的新请求。

关于go - 如何在 go-wrk 命令中发送 post 数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com