0

I am looking to create a web service from an existing API. The existing API is from a separate Windows program that allows the calculation of mileages between 2 more more locations. The API is in C#. There are also different types of calculations (Shortest Distance, Lowest-Cost, etc).

I am confused on how to create a RESTFUL web service that will allow multiple locations. Example in RESTFUL API tutorials show for an example api/books/1/To Kill A Mockingbird/ would get the record for a book. But how would this be for multiple locations? Would it be api/sd/Kansas City,MO/Chicago,IL/Miluwake,WI? How would it know if I'm looking to get the calculations of 2 vs. 10 locations? Could I program it to have 10 locations entered in the URL?

Kevin
  • 309
  • 2
  • 7
  • 17

2 Answers2

1

You don't have to use url segments to accept parameters. You could use a query string parameter with place names, say, semicolon-delimited, then split them out in your code.

/api/sd?cities=Kansas City,MO;Chicago,IL;Miluwake,WI

Then in your code just use Request.QueryString["cities"].Split(';');

Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
1

I think that as your route grows, you may run into other problems such as a url that is too long (there are limits to URL size, from what I understand -- https://stackoverflow.com/a/1051565/507025). If you're going to have and API that can accept an endless number of parameters, I think you would want to do something like sending a JSON object with the request so the API controller can process it.

Additionally, a JSON object might be simpler to process, code wise.

Try here: https://stackoverflow.com/a/20226220/507025

Community
  • 1
  • 1
Josh
  • 547
  • 1
  • 7
  • 21