req.query with single quotes

724 views
Skip to first unread message

mark

unread,
Apr 26, 2011, 4:14:42 AM4/26/11
to Express
Hi,
It seems that req.query is getting truncated when one of the
parameters has a single quote.
For example, from javascript I do a request to mywebsite.com/
param1=o'clock&param2=cupcake, but when I
util.log(util.inspect(req.query)) all I get is ('param1': 'o'}. It
seems that I'm only having this problem from my javascript code,
typing directly into the browser address bar must automatically escape
it.

Any help here?

Thanks.

Laurie Harper

unread,
Apr 26, 2011, 4:45:02 AM4/26/11
to expre...@googlegroups.com

Yes, browsers escape request parameters correctly when submitting HTTP requests, and you should do the same :-)

When you say, "from my javascript code" it's clear if you're talking about client-side or server-side JavaScript... how do you "do a request"? If you're talking about client-side code, are you using an Ajax library? If server-side, which Node module / API are you using to perform the request?

--
Laurie Harper
http://laurie.holoweb.net/

Daniel Shaw

unread,
Apr 26, 2011, 4:28:41 AM4/26/11
to expre...@googlegroups.com
Ran into the same issue recently. Using the full
fixedEncodeURIComponent function client-side did the trick for us:
http://dshaw.me/hDXzdz

No subsequent changes on the backend to your req.query values are necessary.

That's my fix. If anyone has a better explanation as to why, I'm curious.

Daniel Shaw
@dshaw

> --
> You received this message because you are subscribed to the Google Groups "Express" group.
> To post to this group, send email to expre...@googlegroups.com.
> To unsubscribe from this group, send email to express-js+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/express-js?hl=en.
>
>

mark

unread,
Apr 26, 2011, 1:33:36 PM4/26/11
to Express
Yes I was referring to client-side javascript. I'm just basically
doing a window.open to a url. I have a bunch of parameters, and I'm
using jQuery.param() to encode them all.
From the jQuery website: jQuery.param - Create a serialized
representation of an array or object, suitable for use in a URL query
string or Ajax request.

I was assuming that this function would encode anything that needs to
be encoded in a url parameter. It seems to work for everything except
single quotes. Maybe my understanding is wrong. So what additional
encoding do I need to do? Do I need to encode all the url parameters,
and then after that encode the entire url?

Laurie Harper

unread,
Apr 26, 2011, 4:52:43 PM4/26/11
to expre...@googlegroups.com
You may be calling jQuery.param() with invalid data (note that if you pass an array, it must be in the specified format) or otherwise doing something incorrect in constructing the URL. Look at the actual string you're passing to window.open() and verify that it's a correctly encoded URL.

L.

> --
> You received this message because you are subscribed to the Google Groups "Express" group.
> To post to this group, send email to expre...@googlegroups.com.
> To unsubscribe from this group, send email to express-js+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/express-js?hl=en.
>

--
Laurie Harper
http://laurie.holoweb.net/

mark

unread,
Apr 27, 2011, 11:22:33 PM4/27/11
to Express
I'm pretty confident that I'm using it correctly.
See this jsfiddle
http://jsfiddle.net/VtrZR/1/
the single quote is not encoded when it's a url parameter.

encodeURIComponent does the same thing.

Maybe for now I'll just do a string replacement on the client side for
the apostrophe.

Eran Hammer-Lahav

unread,
Apr 28, 2011, 2:38:14 AM4/28/11
to expre...@googlegroups.com

Laurie Harper

unread,
Apr 28, 2011, 5:36:48 AM4/28/11
to expre...@googlegroups.com
It's a combination of that and jQuery not encoding the quote -- which is valid, as pointed out in the issue. My guess is that the browser mark was using *did* encode the quote, hence the difference in behaviour.

L.

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

mark

unread,
Apr 28, 2011, 2:38:16 PM4/28/11
to Express
Hi Eran,
Yes I think this is the underlying issue. I was thinking that it might
have been a bug in node, thanks for pointing it out. I looks like
upgrading from 0.4.6 to 0.4.7 might fix the issue. I will try that.

Mark

On Apr 28, 2:36 am, Laurie Harper <lau...@holoweb.net> wrote:
> It's a combination of that and jQuery not encoding the quote -- which is valid, as pointed out in the issue. My guess is that the browser mark was using *did* encode the quote, hence the difference in behaviour.
>
> L.
>
> On 2011-04-28, at 2:38 AM, Eran Hammer-Lahav wrote:
>
> > Does this explain it?
>
> >https://github.com/joyent/node/issues/954
>
> > EHL
>
> > --
> > You received this message because you are subscribed to the Google Groups "Express" group.
> > To post to this group, send email to expre...@googlegroups.com.
> > To unsubscribe from this group, send email to express-js+...@googlegroups.com.

mark

unread,
Apr 28, 2011, 5:28:45 PM4/28/11
to Express
update: I can confirm that updating to node 0.4.7 solved my problem.

Regards,
Mark
Reply all
Reply to author
Forward
0 new messages