Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How can I make a simple REST/JSON request?

    I am using the latest version of SmartClient and running my app in Chrome. I am developing with SmartClient only, NOT with SmartGWT, so please don't lead me to Java examples, that only confuses me more.

    All I want to do is simply make a POST using REST with a JSON data packet so I can have the server return me the data requested. I have gone through the Quick Start pdf, I have looked at the documentation, and feature list and none of that really is helping me here.

    I just want to make REST request to a URL like so:
    Code:
    http://<ipAddress>/rest/request/<sessionId>
    And send along a JSON packet that my server is expecting like this:
    Code:
    {
       "call": "Data_openFile",
       "params": {
          "filename": "my_example_file.txt",
          "mode": 0
       }
    }
    Then the server in turn returns me a JSON packet like this:
    Code:
    {
       "_rv": 0,
       "filesize": 1337
    }

    This is my code so far:

    Code:
    isc.RestDataSource.create({
    				ID:"getSysType",
    				dataFormat:"json",
    				dataTransport:"xmlHttpRequest", 
    				showPrompt:false,
    				operationBindings:[
    					{operationType:"fetch", dataURL:"/rest/request/"+session, dataProtocol:"postParams"}
    				],
    				transformRequest: function (dsRequest) {
    					dsRequest.contentType = "application/json";
    					//dsRequest.httpMethod = "POST";
    					dsRequest.httpHeaders = { "Accept" : "application/json" };
    				    dsRequest.data = {
    						"call":"SysInfo_getInfo",
    						"params":{
    							"name":"info.system.type"
    						}
    					}                          
    					return isc.addProperties({}, dsRequest.data);
    				}
    			
    			isc.RestDataSource.get("getSysType").fetchData(null, getSysTypeHandler);
    
    function getSysTypeHandler(data)
    		{
    			Log.logWarn("GetSysTypeHandler:"+data);
    		}
    In the debugger console under RPC the DSRequest tab has:
    Code:
    {
        "dataSource":"getSysType", 
        "operationType":"fetch", 
        "data":{
            "call":"SysInfo_getInfo", 
            "params":{
                "name":"info.system.type"
            }
        }, 
        "showPrompt":false, 
        "requestId":"getSysType$5441"
    }
    The Raw tab has:
    Code:
    call=SysInfo_getInfo&params=%7B%22name%22%3A%22info.system.type%22%7D
    The RPCRequest tab:
    Code:
    {
        "actionURL":"/rest/request/97dced9a8e73a2b17f1b8a410dbec9cd", 
        "showPrompt":false, 
        "transport":"xmlHttpRequest", 
        "useSimpleHttp":true, 
        "promptStyle":"dialog", 
        "params":{
            "call":"SysInfo_getInfo", 
            "params":{
                "name":"info.system.type"
            }
        }, 
        "httpMethod":"POST", 
        "contentType":"application/json", 
        "httpHeaders":{
            "Accept":"application/json"
        }, 
        "sendNoQueue":true, 
        "bypassCache":true, 
        "callback":{
            "target":[RestDataSource ID:getSysType]
        }, 
        "serverOutputAsString":true, 
        "data":null
    }

    #2
    The documentation tells you not to use RestDataSource if you're trying to define your own network format or adapt to pre-existing formats. This, and a long, in-depth discussion of how this process works, is in the Client-Server Integration topic. Start from the QuickStart Guide's chapters on Data Integration.

    Comment


      #3
      Ok, I must be dense because I have read the Quick Start guide and the client-side integration in docs and I am missing the mystery combination to make those REST calls with JSON data packages.

      Based on my code I posted above, what is the secret sauce? Do I use "fetch" or do I use "custom"? I am not using RestDataSource. so do I use DataSource? I see that is mentions subclassing DataSource when you want to do what I need to do, but in javascript, what does that mean?

      I've tried all different combinations of the DataSource object by going through the API docs and implementing the variations but to no avail.

      So any other suggestions? Any where else besides the Quick Start guide and docs that I can be pointed to?

      Comment


        #4
        I would like to encode the password field in md5 encrypted in order to transmit to the server.

        Thanks

        Comment

        Working...
        X