Home > Blockchain >  How do I check a string is url encoded or not in golang?
How do I check a string is url encoded or not in golang?

Time:06-22

I dont know what is the source of string it can be url encoded how do I test it whether it is a normal string or url encoded string this using this particular code

func decode(str1 string) {
   //str1 := "elementINPUT61599119[]"
   resl, err := url.QueryUnescape(str1)
   log.Println(err)
   return resl
}

func ifUrlEncoded(string string) bool{
  if //function{
    return true
  }
  return false
}

func main(){
 if ifUrlEncoded(str){
   fmt.Println(decode(str))
 }
else{
 fmt.Println(str)
}
}

CodePudding user response:

If I understand correctly, you want to find out if the string is able to be processed by QueryUnescape before passing it to QueryUnescape? If so, there's no need. If QueryUnescape is unable to process the string you gave, it will return an error.

decoded, err := url.QueryUnescape(originalString)

if err != nil {
    // The string was not able to be decoded this way, so treat it as a "normal" string
    decoded = originalString
}
  • Related