2

I am building a router layer for an API where I take the response and extract a parameter (in this case a service_code, the name can vary - service_code, serviceCode, code need to handle all) from the request body and pass it to the service that needs it. The data can be in raw json format application/json , query params, path params or as form data multipart/form-data. I am able to do it with the others but with form, when I pass the payload to the destination service I get the error multipart: NextPart: EOF.

Here is how I am reading the data:

c.MultipartForm()
postForm := c.Request.PostForm
queryParams := c.Request.URL.Query()
route := c.Request.URL.Path
body, _ := ioutil.ReadAll(c.Request.Body)

serviceCode, destUrl, payload := getCityCode(queryParams, route, body, postForm)

Then I pass this payload to http.NewRequest

http.NewRequest(method, destUrl, strings.NewReader(payload))

At the destination when I read the data:

data := models.Data{}
err := c.Bind(&data); err != nil {
  c.JSON(400, gin.H{"error": err.Error()})
  return
}

The error is multipart: NextPart: EOF. Can someone help please!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Archit Verma
  • 1,911
  • 5
  • 28
  • 46

1 Answers1

-1

This error happens when the form is empty because you are reading all the stream data with ioutil before binding it. You should change your code to use the information you need after binding it.