I am trying to make an http post request with image and parameters in the form-data, but when I am adding image, my parameters are lost.
testProduct := &Product{
Name: "TestProductName",
ImageExtension: "png",
}
var b bytes.Buffer
multipartWriter := multipart.NewWriter(&b)
multipartWriter.CreateFormFile("image", "../test.png")
multipartWriter.Close()
form = url.Values{}
form.Add("name", testProduct.Name)
form.Add("image_extension", testProduct.ImageExtension)
req, _ := http.NewRequest(http.MethodPost, "api/v1/admin/products/", &b)
req.PostForm = form
req.Header.Add("Authorization", "Bearer "+loginResponse.Token)
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
But when the request handled the parameters doesn't bind: https://i.imgur.com/JmT4qLh.png
This is the product struct:
type Product struct {
ID string `form:"id" json:"id"`
Name string `form:"name" json:"name"`
Price int64 `form:"price" json:"price"`
ImageExtension string `form:"image_extension" json:"image_extension"`
}