2

I am performing testing on a functionality which requires following strcuture data to be sent as request's body while performing http request,

type ReqDataFormat struct {
    File           *multipart.FileHeader `form:"file" validate:"required" binding:"required"`
    DivisionID int64                 `form:"division_id" validate:"required" binding:"required"`
}

But the problem is when I try to unparse the incoming request's body into the ReqDataFormat struct var using c.ShouldBind, it returns with the following error:

error="Key: 'ReqDataFormat.File' Error:Field validation for 'File' failed on the 'required' tag\nKey: 'ReqDataFormat.DivisionID' Error:Field validation for 'DivisionID' failed on the 'required' tag"

And if I remove the validate and binding tags from struct then it returns with empty struct fields with no data. Please suggest some solution.

  • I think `json:"file"` should be `form:"file"`. But since the `DivisionID` field has a correct tag and it still is empty, there must be some other issues too. Can you describe how the request is created? (or better, share the request). – Zeke Lu Jun 03 '23 at 05:48
  • I have made a util function for testing purposes which performs http request. In that I am sending the data as io.Reader. Also ```json:"file"``` is infact ```form:"file"```. It was typing error I have fixed it. – Ashwini Kumar Jun 03 '23 at 05:55
  • Please see this [answer](https://stackoverflow.com/a/75989469/1369400) which shows how to send multipart-form request in Golang. See if there is any issue in your util function. – Zeke Lu Jun 03 '23 at 06:33
  • 1
    Thanks for your response, I'll look into that and get back to you. – Ashwini Kumar Jun 03 '23 at 07:17

0 Answers0