how to stream json to http post req

58 views
Skip to first unread message

Alexander Mills

unread,
Sep 2, 2020, 4:48:37 PM9/2/20
to golang-nuts
I have the following code:

```
b := bytes.NewBuffer([]byte(""))

go func() {
for _, v := range z {

jsonStr, err := json.Marshal(&v)

if err != nil {
continue
}

b.Write([]byte(string(jsonStr) + "\n"))
}
}()


fullUrl := fmt.Sprintf("%s/%s", baseUrl, "_bulk")
req, err := http.NewRequest("POST", fullUrl, b)

if err != nil {
 log.Fatal(err)
}

req.Header.Set("Content-Type", "application/json")

c := http.Client{}
resp, err := c.Do(req)


```

is this the correct way to stream the body to the request?

I put the same code here for ease of reading:

-alex

burak serdar

unread,
Sep 2, 2020, 5:16:30 PM9/2/20
to Alexander Mills, golang-nuts
On Wed, Sep 2, 2020 at 2:49 PM Alexander Mills
<alexande...@gmail.com> wrote:
>
> I have the following code:
>
> ```
> b := bytes.NewBuffer([]byte(""))
>
> go func() {
> for _, v := range z {
>
> jsonStr, err := json.Marshal(&v)
>
> if err != nil {
> continue
> }
>
> b.Write([]byte(string(jsonStr) + "\n"))
> }
> }()
>
>
> fullUrl := fmt.Sprintf("%s/%s", baseUrl, "_bulk")
> req, err := http.NewRequest("POST", fullUrl, b)

The request will write whatever is in the buffer and stop there. If
you want to stream data, you can use a pipe:

rd, wr:=io.Pipe()
go func() {
defer wr.Close()
// Write data to wr
}()
req, err:=http.NewRequest("POST",URL,rd)

>
> if err != nil {
> log.Fatal(err)
> }
>
> req.Header.Set("Content-Type", "application/json")
>
> c := http.Client{}
> resp, err := c.Do(req)
>
>
> ```
>
> is this the correct way to stream the body to the request?
>
> I put the same code here for ease of reading:
> https://gist.github.com/ORESoftware/c76359206fc98659725f17491fb30cca
>
> -alex
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9d64d98f-2e7c-408d-99e3-84ee2fbba410n%40googlegroups.com.

Alexander Mills

unread,
Sep 2, 2020, 5:27:21 PM9/2/20
to golang-nuts
thanks that looks correct to me
Reply all
Reply to author
Forward
0 new messages