0

I would like to use golang post request, upload pictures, but I do not want to pass filepath, just want to pass [] byte The following article are not what I need because they are used os.Open golang POST data using the Content-Type multipart/form-data

func Upload(url, file string) (err error) {
    // Prepare a form that you will submit to that URL.
    var b bytes.Buffer
    w := multipart.NewWriter(&b)
    // Add your image file
    f, err := os.Open(file)
    if err != nil {
        return 
    }
    defer f.Close()
    fw, err := w.CreateFormFile("image", file)
    if err != nil {
        return 
    }
    if _, err = io.Copy(fw, f); err != nil {
        return
    }
    // Add the other fields
    if fw, err = w.CreateFormField("key"); err != nil {
        return
    }
    if _, err = fw.Write([]byte("KEY")); err != nil {
        return
    }
    // 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", url, &b)
    if err != nil {
        return 
    }
    // 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 
    }

    // Check the response
    if res.StatusCode != http.StatusOK {
        err = fmt.Errorf("bad status: %s", res.Status)
    }
    return
}
abhink
  • 6,919
  • 1
  • 25
  • 38
xiaoxinmiao
  • 157
  • 1
  • 9
  • The http.request.Body() is a byte stream, maybe your question belongs to the client side, where does these images come from? A website or some other client? – wgoudsbloem Jun 01 '17 at 03:32
  • My request is such that the frontend and api(api1) is separated, the frontend to pass a base64 parameters, in the api(api1): This parameter will be converted to [] byte, and then call another api(api2)by http post – xiaoxinmiao Jun 01 '17 at 04:21
  • Here are the parameters I requested --8D4A8ED669208A7 Content-Disposition:form-data;name="charset" Content-Type:text/plain utf-8 --8D4A8ED669208A7 Content-Disposition:form-data;name="sign" Content-Type:text/plain Qlv1mjWGUEq+7i2FhTGjCa7WwFcZBHlg019JrHgjNJ2D98bEnkHKI9A+scHm+xaewgaqC2i73lybrRNwzUpKJFlUoVcrnFhuMWbHCI73Zkg+mn8lLJrzMgoqPBTsMxze+0uDCci7cmpM8ijTMKaxtwWddg910EzDMkQra14TiU4= --8D4A8ED669208A7 Content-Disposition:form-data;name="image_content";filename="123.jpg" Content-Type:image/jpeg – xiaoxinmiao Jun 01 '17 at 05:03
  • I expect like this: if _, err = io.Copy(fw, []byte); err != nil { return } – xiaoxinmiao Jun 01 '17 at 05:08
  • The answer is already in the sample code, i.e. instead of `io.Copy(fw, f)`, write directly using `fw.Write(your-data)`. – putu Jun 01 '17 at 10:27
  • you are right,@putu – xiaoxinmiao Jun 01 '17 at 13:10
  • I mark answer in another article:https://stackoverflow.com/questions/44302374/can-i-post-with-content-type-multipart-form-data-in-go – xiaoxinmiao Jun 01 '17 at 21:27

0 Answers0