Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: reconsider request.Body #12977

Closed
jon-zu opened this issue Oct 18, 2015 · 4 comments
Closed

net/http: reconsider request.Body #12977

jon-zu opened this issue Oct 18, 2015 · 4 comments

Comments

@jon-zu
Copy link

jon-zu commented Oct 18, 2015

Go Version: 1.5
OS: Linux x86

I'd like to see that the concept of request.Body is reconsidered. While on the server side(you have http.ResponseWriter and you only need to read the reqest.Body) it may be good to use It's annoying for the client side.

A request send from a client always requirs a reader to read from, but this is critical and expensive in multiple ways. Especially if you may upload larger data(ex: a large encoded json, a generated image) you kinda need to find a good encode logic(either you use 2 goroutines or some state machine which can encode up to N bytes per Write call) or you allocate a large buffer encode to it and send the data.

Example:

package main

import (
    "bytes"
    "fmt"
    "io"
    "net/http"
)

type SimpleBufWriter []byte

func (s *SimpleBufWriter) Write(w io.Writer) (int, error) {
    return w.Write([]byte(*s))
}

func main() {
    var (
        buf  = bytes.NewBuffer(nil)
        data = SimpleBufWriter([]byte{0, 1})
    )

    //Tmp buffer is created
    if _, err := data.Write(buf); err != nil {
        fmt.Println("data.Write:", err)
        return
    }

    /*
        Body can be only a io.ReadCloser It would be so much easier to just pass a writer here
        Or something like http.BodyWriter(w) with basicly returns errors for Read and Close but is forwarding Write just for the underlying writer
    */
    req, err := http.NewRequest("POST", "http://example.ex/post", buf)
    if err != nil {
        fmt.Println("NewRequest:", err)
        return
    }

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        fmt.Println("Do:", err)
        return
    }

    defer resp.Body.Close()
}
@bradfitz
Copy link
Contributor

Actually, having Body be an io.Reader is the most efficient design. Plus, the API was frozen 3.5 years ago. It looks like you're still learning Go.

Please discuss this on one of the forums. See https://golang.org/wiki/Questions

@bradfitz
Copy link
Contributor

Hint:

    inMemoryBody = []byte{'0', '1'}
    req, err := http.NewRequest("POST", "http://example.ex/post", bytes.NewReader(inMemoryBody))

@jon-zu
Copy link
Author

jon-zu commented Oct 18, 2015

@bradfitz
I got 1 full yr experience in go could you explain where It looks like I am still learning Go?
Your 'Hint' only works for a byte slice but a json would still require buffering. I can actually make a proof next week that there's a possible way to make this by far more efficent and only using a writer.

However I'll head over to the mailing list then I think.

@bradfitz
Copy link
Contributor

Let me know where you asked the question and I can help out there if others don't beat me to it.

@golang golang locked and limited conversation to collaborators Oct 17, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants