36

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.

Yay295
  • 1,628
  • 3
  • 17
  • 29
user847495
  • 9,831
  • 17
  • 45
  • 48
  • use url encioding. Check out [this post](http://stackoverflow.com/questions/5099869/encode-a-string-for-sending-with-http-request) – Samich Sep 04 '11 at 11:14
  • @Samich, I am using encodeURIComponent(s) – user847495 Sep 04 '11 at 11:15
  • @samich, I tried querystring.stringify(data) instead of encodeURIComponent, but the results are the same...an empty post... – user847495 Sep 04 '11 at 11:18
  • You never show the `encodeURIComponent` function -- maybe you have a bug – Hogan Sep 04 '11 at 11:22
  • @Hogan...that's built into Node.js – user847495 Sep 04 '11 at 11:23
  • 1
    This doesn't directly address the problem, but... `encodeURIComponent` does not encode the single quote (apostrophe) because, according to [RFC 3986](http://www.faqs.org/rfcs/rfc3986.html), 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.) The problem is likely to be at the end receiving the request. – MrWhite Nov 19 '12 at 10:44

4 Answers4

37

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().

nidalpres
  • 3,778
  • 1
  • 18
  • 12
6

I'm doing a similar thing (also with Node.js) and first tried using JavaScript's built-in escape() function, but it didn't really work.

Here's how I ended up getting search to work. It might just be a fluke:

 function doMySearch(showTitle) {
     showTitle = escapeShowTitle(showTitle)
     var url = "http://graph.facebook.com/search?q=" + showTitle + "&type=page"
     doSomethingWith(url)
}

function escapeShowTitle(title) {
    title = title.replace(/'/g, "")
    title = escape(title)
    return title
}

doMySearch("America's Funniest home Videos")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamund Ferguson
  • 16,721
  • 3
  • 42
  • 50
  • 1
    In your case, because you probably want that apostrophe, try escape(). – Jamund Ferguson Oct 27 '11 at 16:28
  • 3
    Also, someone suggested try this: replace(/'/g,"%27") http://stackoverflow.com/questions/2194850/solving-bug-with-single-quotes-in-names-of-friends/2929665#2929665 that didn't work for me though. – Jamund Ferguson Oct 27 '11 at 16:30
5

I know this doesn't address the OP's question, but for those coming here with OData Query related questions, note that the escape character is yet another single quote.

unescapedValue.replace(/'/g, '\'\'')

This assumes you have already performed an encodeURIComponent(unescapedValue) on your string

Source: https://stackoverflow.com/a/4483742/2831961

daniel.caspers
  • 1,660
  • 1
  • 18
  • 22
  • 3
    This was exactly what I needed. Thank you for taking the time to post this for those ending up here from a search engine. – Nick Painter Dec 11 '18 at 22:29
1

Recent answer (2021)

Using JavaScript's URLSearchParams:

console.log(new URLSearchParams({ text: "Who's that girl?" }).toString())

// or

console.log(new URLSearchParams("text=Who's that girl?").toString())
GG.
  • 21,083
  • 14
  • 84
  • 130