I’ve written some OData V4 APIs to pull data from a local SQL database. I’m using curl to query the API. The APIs are ASP.NET and deployed on an IIS server.
If I query my OData API like this, I get the correct data back:
IPADDRESS/cdi_unsubscribes?$filter=contains(cdi_email,'<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="157c7b737a55">[email protected]</a>')
However, if I query my OData with an eq filter like this:
IPADDRESS/cdi_unsubscribes?$filter=cdi_email eq '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f464149406f4a424e4643014c4042">[email protected]</a>'
I get the following error message:
“message”:”The query specified in the URI is not valid. The $filter expression must evaluate to a single boolean value.”
Then curl outputs:
curl: (6) Could not resolve host: eq
curl: (6) Could not resolve host: email.com’
I don’t understand why this is happening.
My WebApiConfig:
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
config.AddODataQueryFilter();
config.Select().Expand().Filter().OrderBy().MaxTop(null).Count().SkipToken();
builder.EntitySet<cdi_unsubscribe>("cdi_unsubscribes");
builder.EntityType<cdi_unsubscribe>().Filter("cdi_email");
builder.EntitySet<Product>("Products");
config.MapODataServiceRoute("ODataRoute", null, builder.GetEdmModel());
}
I’ve added 2 lines to ensure that filtering works on that specific entity on that field. Removing one doesn’t fix this.
My Controller Class:
[HttpBasicAuthorize]
[RequireHttps]
public class cdi_unsubscribesController : ODataController
{
cdi_unsubscribesContext db = new cdi_unsubscribesContext();
private bool cdi_unsubscribeExists(Guid key)
{
return db.cdi_unsubscribes.Any(p => p.id.Equals(key));
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<cdi_unsubscribe> Get()
{
return db.cdi_unsubscribes;
}
[EnableQuery]
public SingleResult<cdi_unsubscribe> Get([FromODataUri] Guid key)
{
IQueryable<cdi_unsubscribe> result = db.cdi_unsubscribes.Where(p => p.id.Equals(key));
return SingleResult.Create(result);
}
My Context Class:
public class cdi_unsubscribesContext : DbContext
{
public cdi_unsubscribesContext() : base("name=cdi_unsubscribesContext")
{
}
public DbSet<cdi_unsubscribe> cdi_unsubscribes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("crm");
}
}
My Entity Definition:
public class cdi_unsubscribe
{
public Guid id { get; set; }
public Nullable<int> statecode { get; set; }
public Nullable<int> statuscode { get; set; }
public String cdi_email { get; set; }
public Nullable<Guid> _cdi_contactid_value { get; set; }
public String cdi_subscriptionchannel { get; set; }
public Nullable<DateTime> createdon { get; set; }
}
Am I missing anything obvious? I would have thought that if contains works perfectly, then a basic eq filter should work just fine. My first guess would be that I’m not writing my eq filter correctly, but every example I see online looks just like mine (unless I’m blind!).
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It turns out that curl was the issue!
Replacing the spaces with %20 like so:
IPADDRESS/cdi_unsubscribes?$filter=cdi_email%20eq%20'<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0898e868fa0858d81898cce838f8d">[email protected]</a>'
worked.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0