1

I need to pass a List of complex objects to a GET-Request that i can filter the results.

The complex object looks like that:

public class RecordFilter
{
    public int PageNumber {get;set;}
    public int MaxRecordsPerPage {get;set;}
    public List<FilterElement> FilterElements {get;set;}
}

public FilterElement
{
    public string Name {get;set;}
    public object Value {get;set;}
    public bool IgnoreCase {get;set;}
}

Now I want to pass this as parameters in a GET-Request like this:

api/test/records?PageNumber=1&MaxRecordsPerPage=10&FilterElements=%7B%22Name%22%3A%22test%22%2C%20%22Value%22%3A%22x%22%2C%20%22IgnoreCase%22%3A%20true%7D

decoded its like this:

api/test/records?PageNumber=1&MaxRecordsPerPage=10&FilterElements={"Name":"test", "Value":"x", "IgnoreCase": true}

It will add an element in the "FilterElements"-List but this element has only the default-Values from the constructor (I'm using the [FromURI])...

How can I pass a List of my objects to the Webservice?

Tobias Koller
  • 2,116
  • 4
  • 26
  • 46

1 Answers1

0

By placing a JSON object in your GET parameter, you are misusing the GET method. According to w3c, GET methods should only be used to retrieve data from the web service (See w3schools.com)

While this may be a matter of style, there is another more practical reason for not putting "payload" in the URL parameter: The URL has length restrictions. The size of the list you are passing may vary and you do not know for sure if the JSON serialized list will always be shorter then the maximum length of an URL (roughly 2000 characters by the way)

My suggestion is that you build a GET and a POST method on your web service. You pass the list of FilterElement to the service by calling the POST method from your client like so:

using (var wb = new WebClient())
{        
    string url = "api/test/records"; //the URL to your web service
    var response = wb.UploadValues(url, "POST", FilterElements); //FilterElements being a list of objects you want to pass
}

You then store the list in the web services session (as shown here). (In this example, primitive types are used but the Session also works for complex types).

In the GET method, you read the list from the session, do your custom filtering and return the filtered list.

Community
  • 1
  • 1
  • this solution is great and exactly what I was looking for! – Tobias Koller Aug 31 '14 at 15:20
  • One last question about the session. I used the session like this: _session=SessionStateUtility.GetHttpSessionStateFromContext(HttpContext.Current); but when i put some values to the session they are not there anymore after I made the next webservice-call. I put this to the global.aspx protected void Application_PostAuthorizeRequest() { HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);} – Tobias Koller Aug 31 '14 at 20:42
  • Inculde 'using System.Web' in your usings. Then use 'HttpContext.Current.Session["FilterElementSorageKey"] = myFilterElementList' to store the data in the POST method. To read the data, use 'List myFilterElementsList = HttpContext.Current.Session["FilterElementSorageKey"]' –  Aug 31 '14 at 23:13
  • thank you. this worked. I made a simple commandline-tool to set the requests and this couldn't handle the sessions properly. After I used the firefox addin HTTPRequester everything worked fine. – Tobias Koller Sep 01 '14 at 17:56
  • 1
    Other than purely for style, why would I want to make a second call to the server? POST can return the results instead of storing them in the session. – Chris Leonard Apr 27 '15 at 14:45