'Passing Odata Query Options in the Request Body

In the Odata 4.01 URL conventions it says that for GET requests with extremely long filter expressions you can append /$query to the resource path of the URL, use the POST verb instead of GET, and pass the query options part of the URL in the request body. If I try that with my service I get back a 404.

Does the /$query endpoint need to be manually created in the back end or is this something odata is supposed to take care of transparently? I've been searching like crazy but I'm having trouble finding anything about how to implement this.



Solution 1:[1]

One way to avoid this is wrapping the request in a batch request

You can resolve long url with $batch query.

Good post from Hassan Habib https://devblogs.microsoft.com/odata/all-in-one-with-odata-batch//

All what you should do is:

  1. Allow batching in Startup.cs

         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
         {  
             app.UseODataBatching(); <---- (1)
             app.UseEndpoints(endpoints =>
             {
                 endpoints.MapControllers();
                 endpoints.Select().Filter().Expand().OrderBy();
                 endpoints.MapODataRoute(
                     routeName: "api",
                     routePrefix: "api",
                     model: GetEdmModel(),
                     batchHandler: new DefaultODataBatchHandler()); <---- (2)
             });
         }
    
  2. Request batch query with body, that contains long url request

POST http://localhost/api/$batch Content-Type: multipart/mixed; boundary=batch_mybatch

body:

--batch_mybatch
Content-Type: application/http
Content-Transfer-Encoding: binary

GET http://localhost/api/students/735b6ae6-485e-4ad8-a993-36227ac82851  HTTP/1.1   <--long url requst
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services

--batch_mybatch

Solution 2:[2]

To support this you add app.UseODataQueryRequest() to your startup somewhere before app.UseRouting()

The framework then transforms ~/$query POST requests into GET requests which are handled by the HttpGet action methods on your controller (source).

  • Documentation is here (although currently not up to date)
  • For a complete sample have a look here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ??????? ??????
Solution 2 kirodge