Posting a large string in a multipart form field seems to 'hang'

We are building up a large amount of data to be posted to a server, using a multipart form.
This is not an image, but a text field

Problem is it seems to get stuck at
`res, err := client.Do(req)

If I use a smaller byte array it works perfectly, but when it gets too large the POST never happens.

I hope this is not too vague :slight_smile:

func postData(data *[]byte, host string) bool {
var b bytes.Buffer
    w := multipart.NewWriter(&b)

    // Add the field
    fw, err := w.CreateFormField("data");

		if err != nil {
        return false
    }

    if _, err = fw.Write((*data)[:]); err != nil {
        return false
    }
    // Don't forget to close the multipart writer.
    // If you don't close it, your request will be missing the terminating boundary.
    w.Close()

    // Now that you have a form, you can submit it to your handler.

    req, err := http.NewRequest("POST", host, &b)
    if err != nil {
        return false
    }
    // Don't forget to set the content type, this will contain the boundary.
    req.Header.Set("Content-Type", w.FormDataContentType())

    // Submit the request
    client := &http.Client{}
    res, err := client.Do(req)



    if err != nil {
        return false
    }
fmt.Println(res.StatusCode)
    // Check the response
    if res.StatusCode != http.StatusOK {
        err = fmt.Errorf("bad status: %s", res.Status)
    }
    return true
}

Code was hacked from an example at http://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data

According to this stackoverflow answer, the multipart writer is closed too early and should be closed after calling req.Header.Set(..., w.FormDataContentType()).)

If this does not help, Iā€™d say try finding out if client.Do() starts failing at some particular amount of data. (That is, is there some upper limit that must not be exceeded.) If there is such a limit, this could give some hint.

And another thought: Does the receiver of the POSTed data perhaps choke on large data, making the client hang.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.