1

I am working on sample application to get employment data from a server using a REST API.

[OperationContract]
[WebGet(UriTemplate = "/employ?id={empIDs}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List GetEmpList(string empIDs);

To get the employ details I called it and it worked fine.

GetEmpList("1");

Above code takes only one ID, but I want multiple employ details. Then to get multiple employ I need to use URL <root>/employ?1d=1&id=41&id=45

But to solve this I called the GetEmpList() API as below

GetEmpList("1&id=41&id=45"); 

But it give me an exception:

System.ServiceModel.EndpointNotFoundException

Message :
There was no endpoint listening at https://sample.com/rest/employ?id=221%26id%3d211%26id%3d%26id%3d1057%26id%3d%26id%3d445 that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

If I hard code URL as

[OperationContract]
[WebGet(UriTemplate = "/employ?id={empIDs}&id={empIDs2}&id={empIDs3}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List GetEmpList(string empIDs, string empIDs2, string empIDs3);

then it works, but the problem is number of employ varies based on request.

My question is how to pass multiple parameter to an UriTemplate?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Umesha MS
  • 2,861
  • 8
  • 41
  • 60

1 Answers1

0

There is no standard way to pass in an array-type argument in a query string, but you can do so yourself fairly easily. Assuming all your employee IDs are going to be integers, probably the easiest way (from a technical and a human-readable standpoint) is to pass them in as a comma-delimited string.

Example (unchanged from your original code):

[OperationContract]
[WebGet(UriTemplate = "/employ?id={empIDs}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List GetEmpList(string empIDs);

Call:

GetEmpList("1,2,13");

In the method implementation, you simply do:

var ids = empIDs.Split(new [] { ',' }).Select(id => Int32.Parse(id));

See also this answer.

Community
  • 1
  • 1
Lars Kemmann
  • 5,509
  • 3
  • 35
  • 67
  • Thanks for u r replay, Server dose not support "," separated value. With out changing the server code can we implement this?. i think the error is coming because of URL encoding. is there any way to avoid parameter encoding. – Umesha MS Feb 20 '14 at 10:36
  • Oh, so you're working on the **client** and not on the **server** code? Then you're limited to whatever the server will support. In this case you might have to call `GetEmpList()` in a loop. :( – Lars Kemmann Feb 20 '14 at 10:47
  • Does the last option that you said works (hard-coding `"/employ?id={empIDs}&id={empIDs2}&id={empIDs3}"`) really return multiple results? I find that a bit hard to believe, but if it does, then I suppose you can write a client yourself that will generate the query string dynamically. – Lars Kemmann Feb 20 '14 at 10:49
  • yes it works if we hard code. if fails only if i pass string with multiple IDs – Umesha MS Feb 20 '14 at 11:01
  • As i told if i call GetEmpList("1&id=41&id=45"); then it will throw exception. in the exception message "&" and = are URL encoded. just check the Exception message in my question. – Umesha MS Feb 20 '14 at 11:03
  • i could not solve this problem. for this i created HttpWebRequest and passed the constructed URL. now i am getting response. – Umesha MS Feb 21 '14 at 14:05
  • I'm glad you got it working. I did a search and it turns out with WCF you *can* control exactly what text is returned, if you use `Stream` as your return type. See here: http://stackoverflow.com/a/9134726/287610 Note this goes for return types, but it's possible you can use it for argument types as well. Worth a try, maybe! – Lars Kemmann Feb 21 '14 at 17:28