'Go elasticsearch bulk insert without using the library

HI i wanted to ask the community since I am not an expert in GO how do I add \n at the end of the request so as not to get an error when inserting a large amount of data into elasticsearch bulk

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"},"status":400}

apply the code what I wrote - just say this is a test

package main

import (
    "bytes"
    json "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

type ElkInsertIndex struct {
    Index string `json:"_index"`
    ID    string `json:"_id"`
}

type ElkBulkInsert struct {
    Index []ElkInsertIndex `json:"index"`
}
type ElkUrl struct {
    Url string `json:"url"`
}

func main() {
    data := ElkBulkInsert{
        Index: []ElkInsertIndex{
            {
                Index: "shut_url",
                ID:    "FFFFFFFFFFFFFFFF",
            },
        },
    }

    js, err := json.Marshal(data)
    testBulk := bytes.NewBuffer(js)
    resp1, err := http.Post("http://127.0.0:9200/_bulk", "application/json", testBulk)
    if err != nil {
        log.Println(err)
    }
    body, err := ioutil.ReadAll(resp1.Body)
    if err != nil {
        log.Println(err)
    }
    fmt.Println(string(body))
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source