等同于Go中的curl –data的HTTP请求负载。

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

equivalent to curl --data in go http request payload

问题

为了在NoSQL存储中设置一个键值对,我需要在Go语言中创建一个等效于以下curl命令的代码:

curl -H "Content-Type: text/xml" --data '[...]' http://localhost:8000/test/testrow/test:testcolumn

我尝试使用以下代码来实现这个目的,但是我不知道如何将二进制数据[]byte(value)设置为POST请求的有效载荷。

func setColumn(table string, key string, col string, value string) {
    url := "http://localhost:8123/" + table + "/" + key + "/" + col
    req, err := http.NewRequest("POST", url, nil)
    req.Header.Set("Content-Type", "application/octet-stream")

    data := []byte(value)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // 处理错误
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

那么,如何将数据有效载荷映射到POST请求中?欢迎提供任何指示。

英文:

For the purpose of setting a K/V in a NoSQL storage, I need to create an equivalent to the following curl command in go:

curl -H "Content-Type: text/xml" --data '[...]' http://localhost:8000/test/testrow/test:testcolumn

I am trying to use something in the lines of the following code for that purpose, although I am unable to find how to set the binary data []byte(value) as a POST payload.

func setColumn(table string, key string, col string, value string) {

url := "http://localhost:8123/" + table + "/" + key + "/" + col
req, err := http.NewRequest("POST", url, nil)
req.Header.Set("Content-Type", "application/octet-stream")

data = []byte(value)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
	// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))

}

So, how to map the data payload within the POST request ? Any indicators are welcome.

答案1

得分: 4

为了发布任何数据,您应该在NewRequest方法中传递值而不是nil

这应该可以工作。

func setColumn(table string, key string, col string, value string) {

    url := "http://localhost:8123/" + table + "/" + key + "/" + col
    data := bytes.NewReader([]byte(value))
    req, err := http.NewRequest("POST", url, data)
    req.Header.Set("Content-Type", "application/octet-stream")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // 处理错误
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

大多数NoSQL服务器都有GO API,使用它们而不是创建自己的HTTP请求。

英文:

In order to post any data you should pass the value instead of nil
in the Newrequest method.

This should work

func setColumn(table string, key string, col string, value string) {

	url := "http://localhost:8123/" + table + "/" + key + "/" + col
	**data := bytes.NewReader([]byte(value))**
	req, err := http.NewRequest("POST", url, **data**)
	req.Header.Set("Content-Type", "application/octet-stream")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		// handle error
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

Most of the NoSQL Servers has GO API use them instad of creating your own HTTP request.

huangapple
  • 本文由 发表于 2015年5月3日 15:17:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/30011242.html
匿名

发表评论

匿名网友

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

确定