Q:

Special Characters handling in REST API SOQL C# HttpClient

My Setup. C#.net Project, Rest Api to Salesforce Dev Org.
Received Error on following Query
SELECT ID FROM PRODUCT2 WHERE ProductCode = 'M5Z55UP#ABC'

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Errors>\n    
<Error>\n        
<errorCode>MALFORMED_QUERY</errorCode>\n        
<message>\nPRODUCT2 WHERE ProductCode = &apos;M5Z55UP\n                                    ^\nERROR at Row:1:Column:52\nline 1:52 mismatched character &apos;&lt;EOF&gt;&apos; expecting &apos;&apos;&apos;</message>\n    </Error>\n</Errors>\n

Apearenty, API request to Salesforce is not Allowing # as a special Character.
my.salesforce.com/services/data/v46.0/query/?q=SELECT ID FROM PRODUCT2 WHERE ProductCode = 'M5Z55UP#ABC'

I tried Url Builder, "\" etc. but did not work.
How to Handle Such Characters? Currently I am stuck with # characters but I am sure there are many more.
Thanks,
Dhananjay

A:

You should encode your query, so it will look somehow like:
SELECT+Id+FROM+PRODUCT2+WHERE+ProductCode+=+%27M5Z55UP%23ABC%27

The spaces can be parsed both with + and %20 encodings.

