Announcement

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

    RestDatasource JSON postMessage

    Hello,

    I'm having a heck of a time trying to figure why I can't seem to post JSON formated data using a rest datasource. My data source configuration is as follows...

    Code:
     
    RestDataSource jDS = new RestDataSource();
    jDS.setFetchDataURL("/json/thing");
    jDS.setAddDataURL("/json/add_thing");
    jDS.setUpdateDataURL("/json/add_thing");
    jDS.setDataFormat(DSDataFormat.JSON);
    jDS.setDataProtocol(DSProtocol.POSTMESSAGE);
    
    DataSourceTextField idField =  new DataSourceTextField("id");
    		idField.setPrimaryKey(true);
    jDS.setFields(idField,
    				new DataSourceTextField("name"),
    				new DataSourceTextField("ipAddress"),
    				new DataSourceTextField("url"),
    				new DataSourceTextField("clientid"),
    				new DataSourceTextField("username"));
    Later on I create a DynamicForm so that I may add new records to my datasource like so...

    Code:
    final DynamicForm form = new DynamicForm();
    		form.setDataSource(jDS);
    		form.setUseAllDataSourceFields(true);
    		form.setAutoFetchData(false);
    		
    		 
    		HeaderItem header = new HeaderItem();  
    		header.setDefaultValue("Registration Form");
    		
    		ButtonItem save = new ButtonItem("save");
    		save.addClickHandler(new ClickHandler() {
    			
    			@Override
    			public void onClick(ClickEvent event) {
    				form.validate(false);
    				form.saveData();
    			}
    		});
    		
    		form.setFields(save);
    This data source configuration works fine for fetching data which is always returned as JSON.

    However when I "save" the data is ALWAYS sent as postParams in the body of the http request like so...

    Code:
    name=asdasd&ipAddress=asdasda&url=235234&clientid=asdasd&username=asdasdas&_operationType=add&_oldValues=%7B%7D&_componentId=isc_DynamicForm_0&_dataSource=isc_EditTingrJson%241_0
    I feel like I'm missing something obvious as the documentation states that the body of the http request should be formated json if the DSProtocol is set to postMessage.

    I've also attempted to encode to json manual by overriding transformRequest like so...

    Code:
    final RestDataSource jDS = new RestDataSource(){
    			@Override
    			protected Object transformRequest(DSRequest dsRequest) {
    				JavaScriptObject jso = dsRequest.getData();
    				String s1 = JSON.encode(jso);
    				dsRequest.setAttribute("data", s1);
    				return super.transformRequest(dsRequest);
    			}
    		};
    and...


    Code:
    final RestDataSource jDS = new RestDataSource(){
    			@Override
    			protected Object transformRequest(DSRequest dsRequest) {
    				JavaScriptObject jso = dsRequest.getData();
    				String s1 = JSON.encode(jso);
    				return s1;
    			}
    		};
    Neither of these approaches worked, however when debugging the resulting value of the s1 variable after running ...
    Code:
    String s1 = JSON.encode(jso);
    was well formatted json minus the DSRequest wrapper of operationType and what not.

    Both of these approaches to transformRequest produced an oddly encoded http request that looked something like...

    Code:
    POST /json/add_thing HTTP/1.1
    Host: 127.0.0.1:8888
    User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 115
    Connection: keep-alive
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    Referer: http://127.0.0.1:8888/ThingServiceUI.html?gwt.codesvr=127.0.0.1:9997
    Content-Length: 1219
    Pragma: no-cache
    Cache-Control: no-cache
    
    0=%7B&1=%0D&2=%20&3=%20&4=%20&5=%20&6=%22&7=n&8=a&9=m&10=e&11=%22&12=%3A&13=%22&14=z&15=x&16=z&17=x&18=c&19=%22&20=%2C&21=%20&22=%0D&23=%20&24=%20&25=%20&26=%20&27=%22&28=i&29=p&30=A&31=d&32=d&33=r&34=e&35=s&36=s&37=%22&38=%3A&39=%22&40=a&41=s&42=d&43=z&44=x&45=c&46=%22&47=%2C&48=%20&49=%0D&50=%20&51=%20&52=%20&53=%20&54=%22&55=n&56=e&57=s&58=s&59=u&60=s&61=U&62=r&63=l&64=%22&65=%3A&66=%22&67=z&68=x&69=c&70=z&71=x&72=c&73=%22&74=%2C&75=%20&76=%0D&77=%20&78=%20&79=%20&80=%20&81=%22&82=c&83=u&84=r&85=r&86=e&87=n&88=t&89=N&90=e&91=s&92=s&93=u&94=s&95=C&96=l&97=i&98=e&99=n&100=t&101=I&102=d&103=%22&104=%3A&105=%22&106=z&107=x&108=c&109=z&110=x&111=c&112=%22&113=%2C&114=%20&115=%0D&116=%20&117=%20&118=%20&119=%20&120=%22&121=n&122=e&123=s&124=s&125=u&126=s&127=U&128=s&129=e&130=r&131=n&132=a&133=m&134=e&135=%22&136=%3A&137=%22&138=z&139=x&140=c&141=z&142=x&143=c&144=z&145=x&146=c&147=%22&148=%0D&149=%7D&Class=String&_operationType=add&_oldValues=%7B%7D&_componentId=isc_DynamicForm_0&_dataSource=isc_EditScannerJson%241_0&replaceAll=null&contains=null&startsWith=null&endsWith=null&convertTags=null&asHTML=null&unescapeHTML=null&toInitialCaps=null&evalDynamicString=null&asSource=null&cssToCamelCaps=null
    Like I said I feel like I'm missing something REALLY obvious so any help would be appreciated.

    Thanks!

    FYI I'm using SmartGWT 2.2.

    #2
    I have exactly the same problem... Did you find an answer?
    Thanks

    Comment


      #3
      The operationBindings override the default dataProtocol setting postParams for everything except fetch. You need to change the operationBindings to use postMessage for the desired operations.

      Comment


        #4
        Ok thanks... it works now :-)
        Do you know how can I send only the data to the server and not all smartGwt infos.

        For the moment the server receive this :
        Code:
        {
          "dataSource":"isc_myDS_0", 
          "operationType":"update", 
          "componentId":"isc_ValuesManager_0", 
          "data":{
            "id":1, 
            "name":name, 
            [...]
          }, 
          "oldValues":{
            [...]
          }
        }
        and I would like to receive only
        Code:
        {
          "id":1, 
          "name":name, 
          [...]
        }
        Thanks!

        Comment


          #5
          I found the solution :
          Code:
          protected Object transformRequest(DSRequest dsRequest) {
            dsRequest.setContentType("application/json");
            return JSON.encode(dsRequest.getData());
          }

          Comment


            #6
            I have the same problem. In my class (extended from RestDataSource), I have set
            Code:
             
            setDataProtocol(DSProtocol.POSTMESSAGE); 
            setDataFormat(DSDataFormat.JSON);   // This only affects download parsing, not upload formatting.
            and
            Code:
                @Override
                public Object transformRequest(DSRequest dsRequest)
                {
                   if (dsRequest.getOperationType().equals(DSOperationType.UPDATE))
                   {
                        dsRequest.setActionURL(MyUpdateUrl);
                        dsRequest.setContentType("application/json");
                        return JSON.encode(dsRequest.getData());
                   }
                   else
                   {
                        dsRequest.setActionURL(MyFetchUrl);
                        return super.transformRequest(dsRequest);
                   }
                }
            In fact whenever I return a string in transformRequest (using getDataAsString() or even a hard-coded string), the resulting text (as seen in fiddler) is something like:
            0=%7B&1=%0D&2=%20&3=%20&4=%20&5=%20 ....

            and if I return a jso I get something like:
            Param1=Value1&Param2=Value2 etc.

            How do I get
            {"Param1":"Value1", "Param2":"Value2"}
            which is what my server is expecting?

            SmartGWT: 2.4, Browser: FF 4.0

            Comment


              #7
              same issue with google cloud endpoints

              Be sure your post includes:

              1. smartgwt 5
              2. browser: last chrome
              3. parse exception. Google cloud endpoint expect json, but ListGrid posts(puts) form-urlencoded message when adding(updating) row

              Comment


                #8
                See above, and the docs: if you want a JSON message posted, you should be setting dataProtocol to json.

                Comment

                Working...
                X