headzoo / surf

Stateful programmatic web browsing in Go.
MIT License
1.48k stars 159 forks source link

Use a File interface to allow alternative source for file upload #79

Open smasher164 opened 6 years ago

smasher164 commented 6 years ago

Currently in order to POST a multipart message, I create a temporary file and upload that. However, instead of requiring a file to be created on disk, there should be a mechanism to allow arbitrary data sources that implement a common interface. I propose https://golang.org/pkg/mime/multipart/#File.

type File interface {
        io.Reader
        io.ReaderAt
        io.Seeker
        io.Closer
}

This exposes enough information to construct the body buffer I use in the http library. This way, I can write a filestring as follows:

type filestring struct {
    *strings.Reader
}
func (fs *filestring) Close() error {
    return nil
}
func NewFileString(s string) *filestring {
    return &filestring{strings.NewReader(s)}
}
godovdm commented 6 years ago

@smasher164 Sorry for posting it here, but I can't find any information. Could you please teach me how to upload files using existing version of surf? Thanks in advance!