Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: mysterious escaped single quote in Uri encoded place name

I'm using PlaceAutocomplete to get a place name, which I then url encode and send to a remote host as part of a GET request (in the URL).

At the remote host, I'm finding that some user devices are sending single quotes that are escaped, which is causing a problem because the place name is sent as part of the URL.

e.g. "Andy's Place" is being sent as "Andy\'s Place"

and the backslash \ is a problem when part of a URL for obvious reasons.

... but mostly, this doesn't happen... e.g. on my own test devices it doesn't... the place is sent/received as "Andy's Place".

I'm trying to go through the chain from start to finish to see where the unintended escaping might be happening. I've tried to summarise the chain below, leaving out some stuff along the way (e.g. I don't only send the placeName, I send it with other stuff too)... I hope I've included all important bits:

Place place = PlaceAutocomplete.getPlace(this, data);
String placeName = (String) place.getName();
EditText_placeName.setText(placeName);
// ... later ...
String newPlaceName = EditText_placeName.getText().toString();
String encodedPlaceName = Uri.encode(newPlaceName);
URL url = new URL(strBaseURL + '/' + encodedPlaceName);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
responseCode = connection.getResponseCode();

Is there anything in the above chain that could introduce an unwanted escape character in the URL on some devices but not others?

As a sort of footnote, I know that sending the data by POST (in the body) rather than GET (as part of the URL) is likely to be better (will probably solve my problem completely), and indeed this is what I'm now moving towards, but I'd still like to know how that \ character is getting through the above chain, at least partly out of curiosity.

like image 322
drmrbrewer Avatar asked Jun 18 '17 14:06

drmrbrewer


People also ask

How do you escape a single quote in a URL query string?

UrlEncode [^]. This will help you escape quote symbol.

How do you put an apostrophe in a URL?

You can navigate to Web pages using several methods, such as clicking a bookmark or typing a URL into a browser's address bar. If the URL you need to type contains an apostrophe, replace the apostrophe with %27 and press "Enter" to navigate to the URL.


1 Answers

OK I figured it out, and the "issue" actually happens after the chain of processing referred to in my original post. I'll detail what's actually happening, just in case it helps anyone else in this situation.

The \' actually appears as a result of the way in which the string was being logged in the node.js app. Whenever a timeout occurs in the node.js app (which only happens occasionally, and as it turns out has nothing to do with the \' issue), I halt processing, and return an error.

For diagnostic purposes I add the URL that led to the timeout to an Error object (either one I create myself or one that exists already), so that I can access that URL again to try to recreate the problem and see what might have been causing the timeout.

So something like:

var err = new Error('oh dear');
err.recreate = theURLString; // the URL for the request that led to the problem
console.log(err);

As it happens, when I log this err to the console (as above), that is when the \' is introduced... i.e. any ' character in theURLString comes out as \'.

So I guess it must be the .toString() implementation of the Error object that is doing this. When I do the following:

console.log(JSON.stringify(err));

... the \' sequence does not appear.

or even just the raw URL string:

console.log(theURLString);

So, the problem (such as it was) was only at the logging stage, and not before. And it didn't happen with every request having ' in the URL, simply because not every request with ' was causing a timeout (generally the problem was just overloaded resources). All the stuff before was just throwing me off track.

like image 126
drmrbrewer Avatar answered Sep 27 '22 17:09

drmrbrewer