how to escape single quote in odata filter uri?
Asked Answered
E

3

7

I tried to escape the single quote when preparing the query in JS this way:

_value.replace(/'/g,'%27')

and this way:

_value.replace(/\'/g,'\\\'');

both doesn't seem to work

You can see an example here: http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$select=Freight,CustomerID&$filter=ShipName+eq+'B's%20Beverages'&$format=json

Does anyone know how to escape the single quote?

Thanks

Ela answered 11/10, 2013 at 13:17 Comment(0)
E
15

The single quote need to be doubled, for instance:

ShipName+eq+'B''sBeverages'

instead of

ShipName+eq+'B'sBeverages'
Ela answered 11/10, 2013 at 13:34 Comment(2)
Thanks! Helped me out, how did you find this out? I couldn't find it anywhere in odata documentation.Khalkha
I don't exactly remember where, I guess some googling and trial & error. Here i found a list of characters that has to be escaped before sending to the server msdn.microsoft.com/en-us/library/aa226544(SQL.80).aspx. However, it would be very tedious to create a URI manually for each special case therefore I started a library to handle that here gist.github.com/mohamed-ali/6944876 and I am open to suggestions on how to impove/extend it.Ela
C
1

Used this code to replace single quote... Its working..

_value.replace(/'/g, '%27%27')
Cosmic answered 12/2, 2016 at 9:41 Comment(0)
B
0

as per example 3 of http://docs.oasis-open.org/odata/odata/v4.01/cs01/part2-url-conventions/odata-v4.01-cs01-part2-url-conventions.html#sec_URLComponents

you need escape a quote with a quote (also, don't forget to handle & : encode to %26 )

so "bit's & bobs" becomes "bit''s %26 bobs"

Banner answered 16/6, 2020 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.