tom
tom

Reputation: 1822

Restful web service GET request parameters

I'm using fiddler to test a Web API service I'm writing.

I know I can pass parameters to a RESTful web service in the querystring with a request like - www.example.com/api/Book?Id=123&category=fiction.

Are there other ways of passing the parameters to the service, while still using a GET.

Upvotes: 1

Views: 6144

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87218

There are many parts of the HTTP request which you can use to pass parameters, namely the URI, headers and body. GET requests don't have bodies (some frameworks actually allow that, but they're not common so for all purposes, let's just assume that they can't), so you're limited to the headers and the URI.

In the URI you can pass parameters in different places:

  • Query string (as you're already doing)
    • Ex.: www.example.com/api/Book?Id=123&category=fiction
  • Request path
    • Many frameworks will allow you to get parameters to your actions from paths in the request URI. With ASP.NET Web API you'd typically do that using routing
    • Ex.: www.example.com/api/Book/fiction/123
  • In the fragment, or the part of the URI after the # character. See the URI RFC, section 3.5.
    • Ex.: www.example.com/api/Book?Id=123&category=fiction#somethingElse

You can also pass paramters in the HTTP request headers. One parameter which is honored by the ASP.NET Web API is the Accept header, which is used when doing content negotiation. You can also expect custom parameters from those headers, and read them in your actions (or even have value providers which will read them and map them to the parameters in the methods themselves).

Upvotes: 2

Related Questions