# 3.1.1.Evolution of REST/JSON API

* Web service
  * This is used for easy communication over a network between two electronic devices
  * RPC, XML-RPC -> 2003 SOAP (XML-base) -> Google & Yahoo offered JSON/RPC, REST in 2006
* RPC
  * 遠程調用框架
  * 從一台機器 (客戶端)上通過參數傳遞調用另一台機器 (服務器)上的一個函數或方法
  * 使用形式上像調用本地函數 (或方法)一樣調用遠程函數 (或方法)
  * RPC風格的web service跨語言性不佳
* [XML-RPC](https://zh.wikipedia.org/wiki/XML-RPC), JSON-RPC
  * XML-RPC透過向裝置了這個協定的伺服器發出HTTP請求。發出請求的用戶端一般都是需要向遠端系統要求呼叫的軟體
  * [數據格式範例 (請求)](https://zh.wikipedia.org/wiki/XML-RPC)

    ```
    <?xml version="1.0"?>
    <methodCall>
    <methodName>examples.getStateName</methodName>
    <params>
      <param>
          <value><i4>40</i4></value>
      </param>
    </params>
    </methodCall>
    ```
  * [使用範例 (Python)](http://python3-cookbook.readthedocs.io/zh_CN/latest/c11/p06_implement_simple_remote_procedure_call_with_xml_rpc.html)
    * 服務器端

      ```
      from xmlrpc.server import SimpleXMLRPCServer

      class KeyValueServer:
        _rpc_methods_ = ['get', 'set', 'delete', 'exists', 'keys']
        def __init__(self, address):
            self._data = {}
            self._serv = SimpleXMLRPCServer(address, allow_none=True)
            for name in self._rpc_methods_:
            self._serv.register_function(getattr(self, name))

      def get(self, name):
        return self._data[name]

      def set(self, name, value):
        self._data[name] = value

      def delete(self, name):
        del self._data[name]

      def exists(self, name):
        return name in self._data

      def keys(self):
        return list(self._data)

      def serve_forever(self):
        self._serv.serve_forever()

      # Example
      if __name__ == '__main__':
        kvserv = KeyValueServer(('', 15000))
        kvserv.serve_forever()
      ```
    * 客戶端

      ```
      >>> from xmlrpc.client import ServerProxy
      >>> s = ServerProxy('http://localhost:15000', allow_none=True)
      >>> s.set('foo', 'bar')
      >>> s.set('spam', [1, 2, 3])
      >>> s.keys()
      ['spam', 'foo']
      >>> s.get('foo')
      'bar'
      >>> s.get('spam')
      [1, 2, 3]
      >>> s.delete('spam')
      >>> s.exists('spam')
      False
      >>>
      ```
* SOAP ([Simple Object Access Protocol](https://jenhsuan.gitbooks.io/asp-net/content/412introduction-of-soap.html))
  * 數據交換協議規範
  * XML based standard, Document風格的web service有以下問題:
    * Issues of XML based Protocol
    * XML is heavy in terms of network traffic, large payload
    * Parsing of XML is CPU & memory intensive
    * Slow performance of XML on browser front end not desired
    * Mobile devices battery performance reduced due to XML
    * Standards & versions of standards caused confusion and complexity
    * Mobile and Single page application developers were not happy
  * 跨平台, 跨語言
  * Defined:
    * structure of the messages exchanged, security, transaction, other standards
  * [SOAP請求範例](http://www.cnblogs.com/JeffreySun/archive/2009/12/14/1623766.html)

    ```
    POST /WebServices/WeatherWebService.asmx HTTP/1.1

    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3603)
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "http://WebXml.com.cn/getSupportCity"
    Host: www.webxml.com.cn
    Content-Length: 348
    Expect: 100-continue
    Connection: Keep-Alive

    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
          <getSupportCity xmlns="http://WebXml.com.cn/">
              <byProvinceName>广东</byProvinceName>
          </getSupportCity>
      </soap:Body>        
    </soap:Envelope>
    ```
  * 使用範例
    * WSDL: <http://www.cnblogs.com/JeffreySun/archive/2009/12/14/1623766.html>
* JSON ([JavaScript Object Notation](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/1234json-yu-wu-jian-shi-ti.html))
  * <HTTP://JSON.ORG> launch in 2002
  * Google & Yahoo offered JSON/RPC, REST in 2006


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/asp-net/chapter3rest-api-design-development-and-management/31evolution-of-restful-services/311revolution-of-restjson-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
