Page 1 of 1

UrlEncoder with &

Posted: Tue Feb 15, 2011 8:55 pm
by DoubleDutch
The urlencoder doesn't encode & !

See:

Code: Select all

URLEncoder("test&blah")
It should encode it just in case you encode a string with '&' within it and use it as a parameter among other parameters...

EG:

Code: Select all

a$="blah"
b$="spaces here plus this&that"
c$="bling"

Debug("http:\\mydomain.com?param1="+URLEncoder(a$)+"&param2="+URLEncoder(b$)+"&param2="+URLEncoder(c$))
The parameters should be:
param1=blah
param2=spaces here plus this&that
param3=bling
but what we get is:
param1=blah
param2=spaces here plus this
that
param3=bling

Re: UrlEncoder with &

Posted: Wed Feb 16, 2011 12:02 am
by TomS
I don't think this is a bug, since you should pass an URL to that function and not a single parameter.
Anyways:

Code: Select all

Procedure.s URLParamPathEncoder(param.s)
    ProcedureReturn ReplaceString(URLEncoder(param), "&", "%26")
EndProcedure

Re: UrlEncoder with &

Posted: Wed Feb 16, 2011 12:49 am
by DoubleDutch
If that's so then the help should be updated with a note about it, possibly the command optionallt extended for parameter encoding instead of full url lines?

Re: UrlEncoder with &

Posted: Wed Feb 16, 2011 3:06 am
by Lush
Hi,

TomS, I think you got your parentheses wrong in your ProcedureReturn. The inner parentheses should only be around "param":

Code: Select all

ProcedureReturn ReplaceString(URLEncoder(param), "&", "%26")
But then there's another problem. a ReplaceString on the whole query string will also replace the ampersands that separate key/value pairs instead of only replacing the ampersands inside a value.

I think the solution is to use your FullURLEncoder procedure on each separate value, then build the query string just like DoubleDutch did in his example.
BTW, DoubleDutch, your "spaces here plus this&that" should have "%20" instead of spaces.

This is my first post here, I hope it'll be a useful one ;)

Re: UrlEncoder with &

Posted: Wed Feb 16, 2011 9:37 am
by TomS
Cr@p, you're right. Wrote that on the fly :oops:

But you got my intentions wrong.
DoubleDutch used the function only for a single parameter and my code works for that.
There's no way anyone could tell whether an ampersand is a parameter separator or has to be encoded.
This has to be done by the programmer.

Code: Select all

url$ = URLEncoder("path.php?param1="+FullURLEncoder("bla&blub")+"&param2="+FullURLEncoder("ab&c"))
url$ = URLEncoder("path.php?param1=bla%26blub&param2=ab%26c")
URLParamPathEncoder() would have been a better name, eh?
Lush wrote:BTW, DoubleDutch, your "spaces here plus this&that" should have "%20" instead of spaces.
That's what URLEncoder() is for :lol:

Re: UrlEncoder with &

Posted: Wed Feb 16, 2011 10:04 am
by Lush
TomS wrote:
Lush wrote:BTW, DoubleDutch, your "spaces here plus this&that" should have "%20" instead of spaces.
That's what URLEncoder() is for :lol:
Oops, I selected the wrong one. I meant to quote the "spaces here plus this" in the "but what we get is:" quote. Those spaces should have been "%20".

Code: Select all

url$ = URLEncoder("path.php?param1="+FullURLEncoder("bla&blub")+"&param2="+FullURLEncoder("ab&c"))
This won't work either :D
All the "%26" returned by your procedure will be encoded as "%2526" by URLEncoder() ;)

Re: UrlEncoder with &

Posted: Wed Feb 16, 2011 10:20 am
by DoubleDutch
The problem comes if someone who uses a program enters data (with a & in it) that gets put into a string that then gets sent by the program to a website. The result would be wrong. The command should be re-thought or the documentation should be updated with a warning.

For me, as it stands, the urlencoder is a bit useless - I know how to properly write the http:// bit... ;) The pain is the parameters being encoded properly.

So I just use a custom routine to encode parameters and convert anything that isn't a letter into a %xx thingy!

btw: Anyone tried the url encode with a full unicode string - there is a standard to convert unicode domain into asciii domain - would be cool to use this to convert to and from unicode domain names.

I have "http://ɯoɔ.com/" which should convert to/from "http://xn--o-10a3f.com/" ;)

Re: UrlEncoder with &

Posted: Thu Feb 17, 2011 11:30 am
by jesperbrannmark
Since everyone needs this function:
replacestring will not do them all, so i did my own as well.

Code: Select all

Procedure.s replacestrin(st.s,vad.s,till.s)
  If FindString(st.s,vad.s,1)
    For a=1 To Len(st.s)
      If Mid(st.s,a,1)=vad.s 
        final.s+till.s
      Else
        final.s+Mid(st.s,a,1)
      EndIf
    Next
    ProcedureReturn final.s
  Else
    ProcedureReturn st.s
  EndIf
EndProcedure
Procedure.s URLEncode(st.s)
  st.s=URLEncoder(st.s)
  st.s=replacestrin(st.s,"&","%26")
  st.s=replacestrin(st.s,"!","%21")
  st.s=replacestrin(st.s,"*","%2A")
  st.s=replacestrin(st.s,"'","%27")
  st.s=replacestrin(st.s,"(","%28")
  st.s=replacestrin(st.s,")","%29")
  st.s=replacestrin(st.s,";","%3B")
  st.s=replacestrin(st.s,":","%3A")
  st.s=replacestrin(st.s,"@","%40")
  st.s=replacestrin(st.s,"&","%26")
  st.s=replacestrin(st.s,"=","%3D")
  st.s=replacestrin(st.s,"+","%2B")
  st.s=replacestrin(st.s,"$","%24")
  st.s=replacestrin(st.s,",","%2C")
  st.s=replacestrin(st.s,"/","%2F")
  st.s=replacestrin(st.s,"?","%3F")
  st.s=replacestrin(st.s,"#","%23")
  st.s=replacestrin(st.s,"[","%5B")
  st.s=replacestrin(st.s,"]","%5D")
  ProcedureReturn st.s
EndProcedure		

Re: UrlEncoder with &

Posted: Sat Feb 26, 2011 10:46 am
by Fred
This is a good idea, we will add a #PB_URL_Parameter flag to this command.