librelist archives

« back to archive

Character + in a url parameter

Character + in a url parameter

From:
Sébastien Billion
Date:
2013-09-15 @ 23:35
Hi,

To get a datetime in a iso format, I need to avoid that the + change to
nothing.
2013-09-03T00:00:00+02:00 change to 2013-09-03T00:00:00 02:00
How can I redefine the parsing or flask.request.args.get?

Cheers,
Seb

Re: [flask] Character + in a url parameter

From:
Smartboy
Date:
2013-09-15 @ 23:43
I don't think redefining parsing would be a good idea since the + is a
special character in URLs. You'll need to encode it prior to sending the
request to your API. See this document for more info:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy

Smartboy


On Sun, Sep 15, 2013 at 4:35 PM, Sébastien Billion <
sebastien.billion@gmail.com> wrote:

> Hi,
>
> To get a datetime in a iso format, I need to avoid that the + change to
> nothing.
> 2013-09-03T00:00:00+02:00 change to 2013-09-03T00:00:00 02:00
> How can I redefine the parsing or flask.request.args.get?
>
> Cheers,
> Seb
>

Re: [flask] Character + in a url parameter

From:
Sébastien Billion
Date:
2013-09-16 @ 00:18
Hi,

Thank you for your answer but the users won't use %2B instead +
With the +, the function dateutil.parser.parse return the wrong time, so
the result is not the good one.
If I don't find an other solution, I will ask to the users to escape this
special character by themselves.

Seb




2013/9/16 Smartboy <smartboyathome@gmail.com>

> I don't think redefining parsing would be a good idea since the + is a
> special character in URLs. You'll need to encode it prior to sending the
> request to your API. See this document for more info:
> http://www.blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
>
> Smartboy
>
>
> On Sun, Sep 15, 2013 at 4:35 PM, Sébastien Billion <
> sebastien.billion@gmail.com> wrote:
>
>> Hi,
>>
>> To get a datetime in a iso format, I need to avoid that the + change to
>> nothing.
>> 2013-09-03T00:00:00+02:00 change to 2013-09-03T00:00:00 02:00
>> How can I redefine the parsing or flask.request.args.get?
>>
>> Cheers,
>> Seb
>>
>
>

Re: [flask] Character + in a url parameter

From:
Steven Kryskalla
Date:
2013-09-16 @ 00:22
On Sun, Sep 15, 2013 at 5:18 PM, Sébastien Billion
<sebastien.billion@gmail.com> wrote:
> Thank you for your answer but the users won't use %2B instead +
> With the +, the function dateutil.parser.parse return the wrong time, so the
> result is not the good one.
> If I don't find an other solution, I will ask to the users to escape this
> special character by themselves.

Why not clean the data before sending it to the "parse" function?

d = dateutil.parser.parse(flask.request.args.get('date').replace('+', ' '))

Replace the + with a space before parsing it...

-Steve

Re: [flask] Character + in a url parameter

From:
Sébastien Billion
Date:
2013-09-16 @ 02:03
It's not the problem.
flask.request.args.get('date') don't return the '+' but a space and this is
my problem. I want to get the +


2013/9/16 Steven Kryskalla <skryskalla@gmail.com>

> On Sun, Sep 15, 2013 at 5:18 PM, Sébastien Billion
> <sebastien.billion@gmail.com> wrote:
> > Thank you for your answer but the users won't use %2B instead +
> > With the +, the function dateutil.parser.parse return the wrong time, so
> the
> > result is not the good one.
> > If I don't find an other solution, I will ask to the users to escape this
> > special character by themselves.
>
> Why not clean the data before sending it to the "parse" function?
>
> d = dateutil.parser.parse(flask.request.args.get('date').replace('+', ' '))
>
> Replace the + with a space before parsing it...
>
> -Steve
>

Re: [flask] Character + in a url parameter

From:
Steven Kryskalla
Date:
2013-09-16 @ 02:39
On Sun, Sep 15, 2013 at 7:03 PM, Sébastien Billion
<sebastien.billion@gmail.com> wrote:
> It's not the problem.
> flask.request.args.get('date') don't return the '+' but a space and this is
> my problem. I want to get the +

Ah I see. As Smartboy said, if you want to preserve the + that the
user inputs, it needs to be URL encoded, there's no way around that.

http://en.wikipedia.org/wiki/Percent-encoding

If they are entering the date via an HTML form, it should come across
as a literal "+" instead of a space, because the browser will do the
URL encoding for you. The only way it would come across as a space is
if you're entering it into the URL directly.

Compare what your app does with this:

http://example/?date=2013-09-03T00:00:00+02:00

And the URL encoded version:

http://example/?date=2013-09-03T00:00:00%2B02:00

If you're building that URL with python, you can use urllib.quote_plus
to encode special characters like "+" (or use flask's url_for):

>>> urllib.quote_plus("2013-09-03T00:00:00+02:00")
'2013-09-03T00%3A00%3A00%2B02%3A00'

If you're building the URL from javascript, you can use encodeURIComponent:

> encodeURIComponent("2013-09-03T00:00:00+02:00")
"2013-09-03T00%3A00%3A00%2B02%3A00"

It depends how the URL is being generated, not on how flask is
processing it. You have to fix the input before it gets to flask.

-Steve

Re: [flask] Character + in a url parameter

From:
Sébastien Billion
Date:
2013-09-16 @ 03:19
Thank you for your precision. It was a stupid question in fact... I don't
have to be worried because I guess the users will use my API by encoding
the url. I just did my test by hand... I lost my logic during the week
end...
Sorry for that guys and thank you for your time.

Cheers,
Seb


2013/9/16 Steven Kryskalla <skryskalla@gmail.com>

> On Sun, Sep 15, 2013 at 7:03 PM, Sébastien Billion
> <sebastien.billion@gmail.com> wrote:
> > It's not the problem.
> > flask.request.args.get('date') don't return the '+' but a space and this
> is
> > my problem. I want to get the +
>
> Ah I see. As Smartboy said, if you want to preserve the + that the
> user inputs, it needs to be URL encoded, there's no way around that.
>
> http://en.wikipedia.org/wiki/Percent-encoding
>
> If they are entering the date via an HTML form, it should come across
> as a literal "+" instead of a space, because the browser will do the
> URL encoding for you. The only way it would come across as a space is
> if you're entering it into the URL directly.
>
> Compare what your app does with this:
>
> http://example/?date=2013-09-03T00:00:00+02:00
>
> And the URL encoded version:
>
> http://example/?date=2013-09-03T00:00:00%2B02:00
>
> If you're building that URL with python, you can use urllib.quote_plus
> to encode special characters like "+" (or use flask's url_for):
>
> >>> urllib.quote_plus("2013-09-03T00:00:00+02:00")
> '2013-09-03T00%3A00%3A00%2B02%3A00'
>
> If you're building the URL from javascript, you can use encodeURIComponent:
>
> > encodeURIComponent("2013-09-03T00:00:00+02:00")
> "2013-09-03T00%3A00%3A00%2B02%3A00"
>
> It depends how the URL is being generated, not on how flask is
> processing it. You have to fix the input before it gets to flask.
>
> -Steve
>