darklord
darklord

Reputation: 5167

Reading multipart request results in Unexpected EOF error

Go version: 1.6.3 macos

I am trying to write an api to upload an apk file (several MB in most cases) to the server. Here is the client side code:

func syncApk(apkFile *os.File) {
    defer apkFile.Close()
    var buffer bytes.Buffer
    writer := multipart.NewWriter(&buffer)
    defer writer.Close()
    part, err := writer.CreateFormFile("apk", filepath.Base(apkFile.Name()))
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error creating form file: %v\n", err)
        return
    }

    size, err := io.Copy(part, apkFile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error copying apk file data: %v\n", err)
        return
    }
    fmt.Fprintf(os.Stdout, "Copied %v bytes for uploading...\n", size)

    response, err := http.Post("http://localhost:8080/upload", writer.FormDataContentType(), &buffer)

    if err != nil {
        fmt.Fprintf(os.Stderr, "Error making POST request to sync apk: %v\n", err)
        return
    }

    fmt.Fprintf(os.Stdout, "Successfully uploaded apk file: %v\n", response.StatusCode)
}

Server code:

func main() {
    server := http.Server{
        Addr: ":8080",
    }
    http.HandleFunc("/upload", doApkUpload)
    server.ListenAndServe()
}

func doApkUpload(w http.ResponseWriter, r *http.Request) {
    file, _, err := r.FormFile("apk")
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error retrieving apk file: %v\n", err)
        return
    }

    data, err := ioutil.ReadAll(file)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error reading apk file content: %v", err)
        return
    }

    fmt.Fprintf(os.Stdout, string(data))
}

After I run the server on localhost I run client side code, I get:

Copied 1448401 bytes for uploading...
Successfully uploaded apk file: 200

It looks like the multipart file is written correctly. However I see this error on the server side:

Error retrieving apk file: unexpected EOF

Any idea where is the problem? Thanks!

Upvotes: 3

Views: 10193

Answers (1)

Thundercat
Thundercat

Reputation: 120941

The error indicates that the reader expected more data after the end of the request body. That missing data is the trailing boundary end line written by the multipart Close method.

Call the Close method after writing all parts and before posting the form.

func syncApk(apkFile *os.File) {
    defer apkFile.Close()
    var buffer bytes.Buffer
    writer := multipart.NewWriter(&buffer)
    part, err := writer.CreateFormFile("apk", filepath.Base(apkFile.Name()))
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error creating form file: %v\n", err)
        return
    }

    size, err := io.Copy(part, apkFile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error copying apk file data: %v\n", err)
        return
    }
    fmt.Fprintf(os.Stdout, "Copied %v bytes for uploading...\n", size)
    writer.Close()
    response, err := http.Post("http://localhost:8080/upload", writer.FormDataContentType(), &buffer)

    if err != nil {
        fmt.Fprintf(os.Stderr, "Error making POST request to sync apk: %v\n", err)
        return
    }
    defer response.Body.Clse()

    fmt.Fprintf(os.Stdout, "Successfully uploaded apk file: %v\n", response.StatusCode)
}

Upvotes: 9

Related Questions