[SalesForce] Escaping + in Salesforce REST QUERY

I'm trying to escape test+test@test.com email in salesforce REST query.

https://na16.salesforce.com/services/data/v32.0/query/?q=SELECT+Id+FROM+Contact+WHERE+Email='test+test@test.com'

But it replaces + sign with space, before parsing a query. That results in error:

{ message: " Id FROM Contact WHERE Email LIKE 'test\ test@test.com' ^
ERROR at Row:1:Column:40 Invalid string literal 'test\ test@test.com'.
Illegal character sequence '\ ' in string literal." errorCode:
"MALFORMED_QUERY" }

Is it possible to escape + on url, so it's treated correctly when query parsed?

Best Answer

This is not the most elegant solution but I've encountered the same problem before, you're more than welcome to use this:

    strQuery = 'YOUR+QUERY+HERE';
    strQuery = strEndPoint.replace('%','%25');
    strQuery = strEndPoint.replace(' ','%20');
    strQuery = strEndPoint.replace('+','%2B');
    strQuery = strEndPoint.replace('\'','%27');

then add more escape character replacements as necessary depending on the characters you're using in your querystring.

After all that, just append strQuery to 'https://na16.salesforce.com/services/data/v32.0/query/?q='.