Golang io.copy twice on the request body

后端 未结 4 661
一向
一向 2021-02-03 11:33

I am building a blob storage system and i picked Go as the programming language. I create a stream to do a multipart file upload from client to the blob server.

The stre

4条回答
  •  -上瘾入骨i
    2021-02-03 12:29

    We can convert the stream into string and create it again as many times we need. e.g.

    readerStream := your stream from source
    
    buf := new(bytes.Buffer)
    buf.ReadFrom(readerStream)
    rawBody := buf.String()
    
    newReader1 := strings.NewReader(rawBody)
    newReader2 := strings.NewReader(rawBody)
    

    But it will be great if it can be avoided.

    I am not sure it is the best approach. But it worked for me.

提交回复
热议问题