Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a REST url parameter with an ampersand?

Tags:

rest

c#

.net

Using the System.ServiceModel libraries for routing, I have a REST service with a simple template that looks like

"{searchTerm}?opt={someSearchOpt}

So the call would look like:

http://myhost.contoso.com/searchapi/river%20expeditions

to search for the phrase "river expeditions" with no options.

This simply accepts a search phrase and returns results. It works just fine. However, if searching for a phrase that contains a literal ampersand, such as "Lewis&Clark", i tried the obvious of url encoding the ampersand

Lewis%26Clark

but even so the request is never even routed, but the server immediately returns a 400 Bad Request. It is clear that it is being interpreted as a query string delimiter, and makes the request invalid since this particular template is expecting a url parameter and has no preceeding '?' delimiter.

Since these search phrases may have other restricted characters, they are expected to by url encoded by the client, and when they are successfully routed, the REST api calls HttpUtility.UrlDecode on the parameter. So my question is really if there is some technique to getting a url encoded ampersand correctly routed as a rest url parameter and not pre-emptively interpreted and rejected as a query string delimiter?

Update: For the record, if the search term were expected as a query string parameter (not a url parameter), sending Lewis%26Clark works fine. For example

http://myhost.contoso.com/searchapi?searchTerm=lewis%26clark

So to clarify, I am looking to get the same result when using a url parameter in a REST template.

like image 975
mdisibio Avatar asked Nov 04 '22 00:11

mdisibio


1 Answers

This thread has a similar question\solution.

As mentioned in the answer from the above link, I would avoid url encodeding characters in the uri and instead put those in the query string portion.

like image 80
jeuton Avatar answered Nov 12 '22 15:11

jeuton