Assume there is a service called AjaxService with following contract:
After starting of the service we would typically create the javascript proxy (helper class) of the REST service by invoking of following URL:
http://myservicehost.myservice/ajaxproxy.svc/jsdebug
The response shown in browser should be saved as for example myserviceproxy.js and imported as a javascript in your javascript application:
<script src="ServiceProxies/AjaxServiceProxy.js" type="text/javascript"></script>
Note that all three operations distinguish in style attributes.
JSON in Body
Operation DoWork is not a typical REST operation. But that operation is also not a typical SOAP operation. Input parameter will be transferred in the HTTP body in POST-method serialized as JSON (not by using of DataContractSerializer and XmlSerializer. Instead DataContractJsonSerializer will be used.).
In other words the operation DoWork is implicitly styled with following attribute:
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
To demonstrate what happen on the wire take a look on following javascript line of code, which invokes this operation:
AjaxService.DoWork(m_Query.value, SucceededCallback, FailedCallback, "context");
Request
POST /DeclarativeServiceLibrary1/AjaxService.svc/DoWork HTTP/1.1
Accept: */*
Accept-Language: en-US,de-DE;q=0.9,bs-Cyrl;q=0.8,bs-Cyrl-BA;q=0.7,bs;q=0.6,bs-Latn;q=0.4,bs-Latn-BA;q=0.3,hr-HR;q=0.2,hr-BA;q=0.1
Referer: http://localhost:29186/WidgetDemo/Go.htm
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)
Host: 192.168.3.8
Content-Length: 17
Connection: Keep-Alive
Pragma: no-cache
{"searchText":""}
Response
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.20604
X-Powered-By: ASP.NET
Date: Sat, 05 Sep 2009 20:54:25 GMT
Content-Length: 535
{"d":[{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 1","Key":"01","Price":1},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 2","Key":"02","Price":2},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 3","Key":"03","Price":3},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 7","Key":"04","Price":4},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 4","Key":"05","Price":5},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 5","Key":"06","Price":6}]}
REST with JSON
Operation DoRestWork is a REST operation (see attribute WebGet), because arguments will be transferred in request (simple args).
AjaxService.DoRestWork(m_Query.value, SucceededCallback, FailedCallback, "context");
Request
GET /DeclarativeServiceLibrary1/AjaxService.svc/DoRestWork?searchText=%22%22 HTTP/1.1
Accept: */*
Accept-Language: en-US,de-DE;q=0.9,bs-Cyrl;q=0.8,bs-Cyrl-BA;q=0.7,bs;q=0.6,bs-Latn;q=0.4,bs-Latn-BA;q=0.3,hr-HR;q=0.2,hr-BA;q=0.1
Referer: http://localhost:29186/WidgetDemo/Go.htm
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)
Host: 192.168.3.8
Connection: Keep-Alive
Response
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.20604
X-Powered-By: ASP.NET
Date: Sat, 05 Sep 2009 20:56:01 GMT
Content-Length: 535
{"d":[{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 1","Key":"01","Price":1},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 2","Key":"02","Price":2},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 3","Key":"03","Price":3},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 7","Key":"04","Price":4},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 4","Key":"05","Price":5},{"__type":"MyEntity:http:\/\/daenet.eu","Description":"Product 5","Key":"06","Price":6}]}
REST with XML
Operation DoRestWorkXml is REST operation but response and request will be transferred as XML. Because i our example we use one input argument only searchTest, the argument will be transferred in request’s URI. Usually, more complicated entities would be transferred in the body in specified format.
AjaxService.DoRestWorkXml(m_Query.value, SucceededCallback, FailedCallback, "context");
Request
GET /DeclarativeServiceLibrary1/AjaxService.svc/DoRestWorkXml?searchText=%22%22 HTTP/1.1
Accept: */*
Accept-Language: en-US,de-DE;q=0.9,bs-Cyrl;q=0.8,bs-Cyrl-BA;q=0.7,bs;q=0.6,bs-Latn;q=0.4,bs-Latn-BA;q=0.3,hr-HR;q=0.2,hr-BA;q=0.1
Referer: http://localhost:29186/WidgetDemo/Go.htm
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)
Host: 192.168.3.8
Connection: Keep-Alive
Response
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.20604
X-Powered-By: ASP.NET
Date: Sat, 05 Sep 2009 21:21:48 GMT
Content-Length: 628
<ArrayOfMyEntity xmlns="http://daenet.eu" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><MyEntity><Description>Product 1</Description><Key>01</Key><Price>1</Price></MyEntity><MyEntity><Description>Product 2</Description><Key>02</Key><Price>2</Price></MyEntity><MyEntity><Description>Product 3</Description><Key>03</Key><Price>3</Price></MyEntity><MyEntity><Description>Product 7</Description><Key>04</Key><Price>4</Price></MyEntity><MyEntity><Description>Product 4</Description><Key>05</Key><Price>5</Price></MyEntity><MyEntity><Description>Product 5</Description><Key>06</Key><Price>6</Price></MyEntity></ArrayOfMyEntity>
Posted
Sep 06 2009, 12:28 AM
by
Damir Dobric