Announcement

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

    RestDataSource and setting Authorization token through request header

    I'm trying to use RestDataSource to fetch data using REST APIs.
    The REST API expects the caller to pass session id through 'Authorization:<sessionId>' request header.
    How to set this for RestDataSource(). I don't seem to find it in RestDataSource javadoc.
    Thanks for the help!

    #2
    These can be set via operationBinding -> requestProperties -> httpHeaders to have them be used for every REST request of a particular operationType, or for specific calls to the server, via the requestProperties argument of APIs such as ListGrid.fetchData().

    Comment


      #3
      Can you kindly provide sample code? Thanks!

      Comment


        #4
        Please go ahead and use the API doc to attempt to write the code. If you have trouble, you can post your code here for help, or you can make use of our Consulting service - they can write code for you, on demand.

        Comment


          #5
          I tried the following code and got exception.
          Do you see anything wrong in my code? Thanks!

          OperationBinding[] bindings = restds.getOperationBindings();
          for (int i = 0; i < bindings.length; i++) {
          if (bindings[i].getOperationType() == DSOperationType.FETCH) {
          DSRequest dsRequest = bindings[i].getRequestProperties();
          Map headers = dsRequest.getHttpHeaders();
          headers.put("Authorization", "dummy-session-id");
          dsRequest.setHttpHeaders(headers);
          }
          }

          Uncaught exception escaped : com.google.gwt.event.shared.UmbrellaException
          Exception caught: (TypeError) @com.smartgwt.client.util.JSOHelper::setAttribute(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)([null, string: 'httpHeaders', JavaScript object(7159)]): elem is null
          See the Development console log for details.
          Register a GWT.setUncaughtExceptionHandler(..) for custom uncaught exception handling.
          Last edited by mshen; 29 Jan 2014, 20:43.

          Comment


            #6
            Requests from a RestDataSource do not use dataProtocol:"iscServer" so they should not be directed to the IDACall servlet.

            If you are trying to test out your customized RestDataSource against the SmartGWT server, you would direct the request to the RESTHandler servlet. Although we cannot see a purpose in doing this - when you have the SmartGWT server installed, there is no reason to use RestDataSource.

            Comment


              #7
              I'm testing out RestDataSource with a REST API server (not SmartGWT's rest server). For some reason, we need to implement the REST API server in C++, therefore, we cannot use SmartGWT's rest server.

              The REST API server requires an authorization header for each request. How do I achieve this through RestDataSource?

              Thanks.

              Comment


                #8
                You've already done that. The fact that IDACall rejects your request does not indicate that you have a problem with your Authorization header. It will reject a request from a RestDataSource regardless, as we explained above.

                If you want to check that the Authorization header is present, use a tool such as Firebug to look at the HTTP headers.

                Comment


                  #9
                  Sorry. I updated the stack trace in my previous post. I'm now getting the following exception:

                  Uncaught exception escaped : com.google.gwt.event.shared.UmbrellaException
                  Exception caught: (TypeError) @com.smartgwt.client.util.JSOHelper::setAttribute( Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)([null, string: 'httpHeaders', JavaScript object(7159)]): elem is null
                  See the Development console log for details.
                  Register a GWT.setUncaughtExceptionHandler(..) for custom uncaught exception handling.

                  The code failed to set 'httpHeaders'.

                  dsRequest.getHttpHeaders();
                  dsRequest.setHttpHeaders();

                  complaining about 'elem' being null.
                  Last edited by mshen; 29 Jan 2014, 20:59.

                  Comment


                    #10
                    Your code assumes there are already requestProperties or httpHeaders configured, and crashes if there none. You should just call the setters in this case.

                    Comment


                      #11
                      Thanks a lot for the help. It's working now!

                      Comment

                      Working...
                      X