title: Go elasticsearch bulk insert
body: <p>I have been unable to solve the problem into elasticsearch Bulk method for several days, since I am not strong in Go and started learning it not so long ago, while executing the code :</p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;bytes&quot;
      json &quot;encoding/json&quot;
      &quot;fmt&quot;
      &quot;io/ioutil&quot;
      &quot;log&quot;
      &quot;net/http&quot;
  )
  
  type BulkInsertMetaData struct {
      Index []BulkInsertIndex `json:&quot;index&quot;`
  }
  type BulkInsertIndex struct {
      Index string `json:&quot;_index&quot;`
      ID    string `json:&quot;_id&quot;`
  }
  
  type BulInsertData struct {
      Url string `json:&quot;url&quot;`
  }
  
  func main() {
      dataMeta := BulkInsertMetaData{
          Index: []BulkInsertIndex{
              {
                  Index: &quot;Test&quot;,
                  ID:    &quot;1234567890&quot;,
              },
          },
      }
      data := BulInsertData{
          Url: &quot;http://XXXX.XX&quot;,
      }
      TojsBulInsertData, _ := json.Marshal(data)
      TojsBulkInsertMetaData, _ := json.Marshal(dataMeta)
      BulkMetaData := bytes.NewBuffer(append(TojsBulkInsertMetaData, []byte(&quot;\n&quot;)...))
      BulkData := bytes.NewBuffer(append(TojsBulInsertData, []byte(&quot;\n&quot;)...))
      log.Println(BulkMetaData)
      log.Println(BulkData)
      respMetaData, err := http.Post(&quot;http://127.0.0.1:9200/_bulk&quot;, &quot;application/json&quot;, BulkMetaData)
      if err != nil {
          log.Println(err)
      }
      body, err := ioutil.ReadAll(respMetaData.Body)
      if err != nil {
          log.Println(err)
      }
      fmt.Println(string(body))
      respBulkData, err := http.Post(&quot;http://127.0.0.1:9200/_bulk&quot;, &quot;application/json&quot;, BulkData)
      if err != nil {
          log.Println(err)
      }
      body2, err := ioutil.ReadAll(respBulkData.Body)
      if err != nil {
          log.Println(err)
      }
      fmt.Println(string(body2))
  }
  </code></pre>
  <p>but i get an error:</p>
  <pre><code>2022/02/09 14:37:02 {&quot;index&quot;:[{&quot;_index&quot;:&quot;Test&quot;,&quot;_id&quot;:&quot;1234567890&quot;}]}
  
  2022/02/09 14:37:02 {&quot;url&quot;:&quot;http://XXXX.XX&quot;}
  
  {&quot;error&quot;:{&quot;root_cause&quot;:[{&quot;type&quot;:&quot;illegal_argument_exception&quot;,&quot;reason&quot;:&quot;Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [START_ARRAY]&quot;}],&quot;type&quot;:&quot;illegal_argument_exception&quot;,&quot;reason&quot;:&quot;Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [START_ARRAY]&quot;},&quot;status&quot;:400}
  </code></pre>
  <p>please help and explain what I'm doing wrong, I searched the Internet for the answer to my question but did not find
  I test insert when using REST client passes without problems</p>