Symptom
This document has been created to outline the standardized methods of filtering OData API output based values that contain special characters.
You perform Odata API Request and are performing $filter= operation on a field in which the value contains special characters:
Example Response (JSON):
Result:
{
"error": {
"code": "ServerErrorException",
"message": {
"lang": "en-US",
"value": "String index out of range: 68"
}
}
}
Example Response (XML):
<error>
<code>ServerErrorException</code>
<message>
<lang>en-US</lang>
<value>String index out of range: 68</value>
</message>
</error>
Environment
- SuccessFactors BizX
- OData API
Reproducing the Issue
To reproduce the issue, the below actions can be performed:
- Example: a user with a name similar to the following: Admin O'Data --> firstName eq 'Admin' lastName='O'Data'
- Example API Request: https://apiX.successfactors.com/odata/v2/User?$filter=firstName eq 'Admin' and lastName eq 'O'Data'
- An error similar to those outlined in the above 'Symptom' section, will be returned
There is an apostrophe contained in the value of lastName that the filter operation is being applied to.
Essentially the request would looking for a value of 'O' in the above API request example,
The Data' part of the call that follows, would be considered bad syntax for the API request
Cause
In OData there is handling built in for special characters,
This has to be used when searching for field values that may contain a special characters
Resolution
For apostrophe, use two apostrophe to escape it:
- Example: $filter=lastName eq 'O'Data' ---> $filter=lastName eq 'O''Data' (this will return the User record when applied to the above call)
For '#', use %23:
- Example: $filter=division eq 'ABCD#' ---> $filter=division eq 'ABCD%23'
For '&', use %26:
- Example: $filter=department eq 'R&D' ---> $filter=department eq 'R%26D'
For other special characters:
- Use raw value
Keywords
- Special Characters, OData, API, Request, Query, #, &, Apostrophe