ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.38k stars 1.64k forks source link

How to work with array in get query parameter. Something like `url/controller-path?id[]=2` #1674

Closed Abhinav1217 closed 1 year ago

Abhinav1217 commented 1 year ago

Expected Behavior / New Feature

Hi, I searched for some discord or other platform for Q/A but I couldn't find anything like that, hence asking for solution here. It may be a missing feature because I did find some issues about optional parameters, but nothing was specifically asking for array in parameters.

I am trying to get a List as a query parameter. Something like url/controller-path?id[]=2&id[]=3&id[]=4. The problem is when I try using following in config "UpstreamPathTemplate": "/api/dashboard/carer?id[]={Ids}", the gateway crashes. If I remove the square brackets, then it will work. But I need to support an array of integers.

Actual Behavior / Motivation for New Feature

Should be able to forward parameter array to mvc controller which does support array params and optional params.

Steps to Reproduce the Problem

  1. Basically create a controller path which accepts a array of values, like result from checkboxes, etc

Specifications

raman-m commented 1 year ago

Hi Abhinav! Thanks for your interest in Ocelot gateway!


<PackageReference Include="Ocelot" Version="18.0.0" />

You should use v19 as .NET 7 release. Usage of old releases is on your own risks.


Current implemented "placeholders" logic/features:


Actual Behavior / Motivation for New Feature

Should be able to forward parameter array to mvc controller which does support array params and optional params.

To fix your problem with query params forwarding you have to use Routing feature in advance:

Please use these 2 route definition techniques and you "will be able to forward parameter array to mvc controller"!


I am trying to get a List as a query parameter. Something like url/controller-path?id[]=2&id[]=3&id[]=4. The problem is when I try using following in config "UpstreamPathTemplate": "/api/dashboard/carer?id[]={Ids}", the gateway crashes.

Such kind of placeholders are not supported by Ocelot. Wrong usage of current placeholders! I believe you need to define your route like this:

{
    "DownstreamPathTemplate": "/url/{everything}",
    "UpstreamPathTemplate": "/api/dashboard/{everything}"
}

Maybe definition of a concrete route will be required. But you have to wrap query params by {everything}!


If I remove the square brackets, then it will work. But I need to support an array of integers.

It seems you've misused square brackets! ASP.NET API app binding logic understands array values in query params without square brackets! It must be: url/controller-path?id=2&id=3&id=4 ! More here: Parameter binding in Minimal API applications | Microsoft Learn So, your query string is invalid, ASP.NET pipeline params binder cannot parse it!

To support array of integers you have to wrap query params by {everything} in Routes feature!


Hope it helps!

raman-m commented 1 year ago

Let's find some workaround within one week please! Your issue will be accepted or declined until July 10.

Abhinav1217 commented 1 year ago

Someone from reddit gave me the solution. Query parameters are not required to be mentioned on ocelot, So I just wrote /api/dashboard and pointed it to /api/MobileDashboard and it worked. All the Get Parameters are passed as is. This helped me cleanup my ocelot.json a lot.

That person also pointed me to https://github.com/ThreeMammals/Ocelot/issues/797 for another problem I was facing where I have an optional REST path which at present is not supported by ocelot.