Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass an apostrophe through a URL?

I'm using Node.js:

var s = 'Who\'s that girl?'; var url = 'http://graph.facebook.com/?text=' + encodeURIComponent(s);  request(url, POST, ...) 

This does not work! And Facebook cuts off my text...

Full code:

function postToFacebook(fbid, access_token, data, next){     var uri = 'https://graph.facebook.com/'+String(fbid)+'/feed?access_token='+access_token;     var uri += '&' + querystring.stringify(data);     request({         'method':'POST',         'uri': uri,     },function(err,response,body){         next();     }); };   app.get('/test',function(req,res){     var d = {         'name':'Who\'s that girl?',         'link': 'http://example.com',         'caption': 'some caption...',         'description': 'some description...',         'picture': 'http://i.imgur.com/CmlrM.png',     };     postToFacebook(req.user.fb.id, req.user.fb.accessToken, d);     res.send('done'); }); 

Facebook gets a blank post on the wall. No text shows. Nothing.

When I log my URI, it is this:

https://graph.facebook.com/1290502368/feed?access_token=2067022539347370|d7ae6f314515c918732eab36.1-1230602668|GtOJ-pi3ZBatd41tPvrHb0OIYyk&name=Who's%20that%20girl%3F&link=http%3A%2F%2Fexample.com&caption=some%20caption...&description=some%20description...&picture=http%3A%2F%2Fi.imgur.com%2FCmlrM.png 

Obviously if you take a look at that URL, you see that the apostrophe is not being encoded correctly.

like image 831
user847495 Avatar asked Sep 04 '11 11:09

user847495


People also ask

Are there apostrophes in URLs?

RFC 3986 (updates RFC1738) states that the apostrophe is a reserved character. Reserved chars need only be encoded if they have special meaning in the URI scheme. The apostrophe does not have special meaning in HTTP and does not need to be encoded.

How do you send a single quote in a URL?

encodeURIComponent does not encode the single quote (apostrophe) because, according to RFC 3986, the apostrophe does not need to be encoded in any part of the URL. (It is a 'reserved' character, but carries no special meaning in this context.)

What is a %20 in a URL?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.


1 Answers

Had the same problem, encodeURIComponent didn't encode single quote. The trick is to do the replacement of ' with %27, after the encoding:

var trackArtistTitle = encodeURIComponent("Johnny Vegas - Who's Ready Fo'r Ice Cre'am") // result: Johnny%20Vegas%20-%20Who's%20Ready%20Fo'r%20Ice%20Cre'am trackArtistTitle = trackArtistTitle.replace(/'/g, '%27') // result: Johnny%20Vegas%20-%20Who%27s%20Ready%20Fo%27r%20Ice%20Cre%27am 

This way, trackArtistTitle will be properly decoded on server i.e. with PHP using urldecode().

like image 136
nidalpres Avatar answered Sep 25 '22 22:09

nidalpres