Search code examples
odata

ODATA: Special Character in URL


I am facing an issue in creating a ODATA URL.

For the Following URL,

https://xxxxxxx.xxx.xxxxxxxx.com/sap/c4c/odata/v1/c4codataapi/CorporateAccountHasContactPersonCollection?$filter=AccountID eq '1000024'

Result :

- <m:properties>
    <d:ObjectID>00163E10AD0B1ED686EF458B4E8C51D5</d:ObjectID> 
    <d:ParentObjectID>00163E10AD201EE5A4F0B592DE751AE8</d:ParentObjectID
    <d:AccountID>1000024</d:AccountID> 
    <d:ContactID>1002636</d:ContactID>
    <d:FunctionCode>Z021</d:FunctionCode> 
    <d:Mobile>+33 123456789</d:Mobile> 
    <d:Phone>+33 987654321</d:Phone> 
  </m:properties>

Same for result when i change filter to FunctionCode https://xxxxxxx.xxx.xxxxxxxx.com/sap/c4c/odata/v1/c4codataapi/CorporateAccountHasContactPersonCollection?$filter=FunctionCode eq 'Z021'

But When I search with filter Phone

https://xxxxxxx.xxx.xxxxxxxx.com/sap/c4c/odata/v1/c4codataapi/CorporateAccountHasContactPersonCollection?$filter=Phone eq '+33 123456789'

URL doesnt work at all. Is it becuase of special character "+" in Phone Number

I tried with $filter=endswith(Phone, '123456789'), It worked fine. But this is not solution i am looking for.

Can any one suggest another ways?

Thank you Regards Prat


Solution

  • The reason is that these special characters has different meaning when used in URLs. The JavaScript “encodeUri” or “encodeUriComponent” does not solve this problem.

    Here is the list of the special characters that needs to be replaced when used in the OData queries: https://msdn.microsoft.com/en-us/library/aa226544(SQL.80).aspx

    The special character + Indicates a space (and spaces cannot be used in a URL) so you need to replace the character's with its hexadecimal value, in this case %2B.

    Your corrected filter should then be $filter=Phone eq '%2B33 123456789'.

    Find a good article here.