0

I am using gin gonic as my API Frame work. I currently have a weird issue going on where gin usually strips out some of my characters. Here is my model

 type ConfirmationCode struct {
        Code        string `form:"code" json:"code" xml:"code" binding:"required"`
        PhoneNumber string `form:"number" json:"number" xml:"number" binding:"required"`
        RequestId   string `form:"request_id" json:"request_id" xml:"request_id" binding:"required"`
    }

Here is my main func

func main(){
    router.POST("/codes/confirm", func(c *gin.Context) {
        confirmCode(c)
    })
}

Sample POST Request

/codes/confirm?api_key=xxxx&api_secret=xxx&request_id=a70a917406bd4f9
fb81fad0400ac535b&code=950762&number=+2347*********

Here is my Handler Func

   func confirmCode(c *gin.Context) {
        var confirmationCode ConfirmationCode
        if err := c.ShouldBind(&confirmationCode); err != nil {
            m := err.Error()
            if m == "EOF" {
                m = "Please provide a valid code"
                spitBadRequest(m, c)
                return
            }
            for _, fieldErr := range err.(validator.ValidationErrors) {
                spitBadRequest(constants.ValidationErrorMap[fieldErr.StructField()]+" is required", c)
                return
            }
            return
        }
        receivedNumber:=confirmationCode.PhoneNumber
        log.Println(receivedNumber)
        //receivedNumber is always coming out as 2347********* without the + sign.
        //How can I make sure that the + sign is preserved and not stripped out.
}
mekings
  • 361
  • 3
  • 17

1 Answers1

1

Well, URL's are URL encoded, and a + is a URL encoded space. So, if you want the + to be preserved, you most likely just need to URL encode it, which would be %2B.

&number=%2B2347*********
dave
  • 62,300
  • 5
  • 72
  • 93