3

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!

2
  • 1
    I think you need to add defer response.Body.Close() after http.Post in client side code.
    – putu
    May 14, 2017 at 0:38
  • @putu You have right. I added the line but it doesn't solve the problem.
    – darklord
    May 14, 2017 at 1:47

1 Answer 1

9

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)
}
1
  • Ah! An explicit close, in the correct location, instead of a deferred close solved my own problem. Thank you!
    – SunSparc
    Jul 11 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.