http.Post data-binary, curl equivalent in golang

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

http.Post data-binary, curl equivalent in golang

问题

我正在尝试使用net/http将一个JSON文件发送到ElasticSearch。通常在Curl中,我会执行以下操作:

curl -XPOST localhost:9200/prod/aws -d @aws.json

在Go语言中,我使用了一个示例,但它没有起作用。我可以看到它在发送,但肯定有一些设置不正确。我已经测试了我使用的JSON文件,它是可用的。

Go代码:

  target_url := "http://localhost:9200/prod/aws"
  body_buf := bytes.NewBufferString("")
  body_writer := multipart.NewWriter(body_buf)
  jsonfile := "aws.json"
  file_writer, err := body_writer.CreateFormFile("upfile", jsonfile)
  if err != nil {
    fmt.Println("写入缓冲区时出错")
    return
  }
  fh, err := os.Open(jsonfile)
  if err != nil {
    fmt.Println("打开文件时出错")
    return
  }
  io.Copy(file_writer, fh)
  body_writer.Close()
  http.Post(target_url, "application/json", body_buf)
英文:

I'm trying to use net/http to post a json file to ElasticSearch. Normally in Curl I would do the following:

curl -XPOST localhost:9200/prod/aws -d @aws.json

In golang I've used an example but it has not worked. I can see it posting but something must be set incorrectly. I've tested the JSON file I am using and it's good to go.

Go code:

  target_url := "http://localhost:9200/prod/aws"
  body_buf := bytes.NewBufferString("")
  body_writer := multipart.NewWriter(body_buf)
  jsonfile := "aws.json"
  file_writer, err := body_writer.CreateFormFile("upfile", jsonfile)
  if err != nil {
    fmt.Println("error writing to buffer")
    return
  }
  fh, err := os.Open(jsonfile)
  if err != nil {
    fmt.Println("error opening file")
    return
  }
  io.Copy(file_writer, fh)
  body_writer.Close()
  http.Post(target_url, "application/json", body_buf)

答案1

得分: 7

如果你想从文件中读取 JSON,可以使用以下代码:

jsonStr, err := ioutil.ReadFile("filename.json")
if err != nil {
    panic(err)
}

在 HTTP POST 请求中以简单的方式发送 JSON。

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
    panic(err)
}
defer resp.Body.Close()

fmt.Println("response Status:", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))

这应该可以工作。

英文:

If you want to read json from file then use .

jsonStr,err := ioutil.ReadFile("filename.json")
if(err!=nil){
    panic(err)
}

> Simple way to post json in http post request.

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))

This should work

答案2

得分: 3

请注意,您可以使用io.Reader作为请求体进行Post请求(参考:http://golang.org/pkg/net/http/#Post):

file, err := os.Open("./aws.json")
resp, err := http.Post(targetUrl, "application/json", file)
// TODO: 处理错误

如果文件非常大那么这种方法可能比先将文件内容读入内存更有效

<details>
<summary>英文:</summary>

Note that you can [`Post` with an `io.Reader` as the body](http://golang.org/pkg/net/http/#Post):

    file, err := os.Open(&quot;./aws.json&quot;)
    resp, err := http.Post(targetUrl, &quot;application/json&quot;, file)
    // TODO: handle errors

This might work better than reading the file contents into memory first, especially if the file is very large.

</details>



huangapple
  • 本文由 发表于 2015年4月9日 12:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/29529926.html
匿名

发表评论

匿名网友

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

确定