• Issue with the GET request to the Domino Data Service

    By Jeffrey M Winkler 1 decade ago
    I am using the example from the GET request to the Domino Data Service below.  I changed the URL: to access a database on my server.  I tested the URL and made sure that it actually returns JSON data and it works.  But when I run the code below it does not return any data. 
    
     
    
    I cannot seem to get to the data.  What am I doing wrong?  Also, if I append the URL with/unid/<unid of the view> the conn returns 404.  But if I test the same URL in another browser window it returns data. 
    

    Also, is it possible to bind this to an objectData type?

     

    // Establish connection with Domino database collection resource
    
    var url = new java.net.URL("http://ibm-z0shi2lw4cz.swg.usma.ibm.com:80/api/data");
    
    var conn:java.net.HttpURLConnection = url.openConnection();
    
    conn.setRequestProperty("Accept", "application/json");
    
    if (conn.getResponseCode() == "200") {
    
    
    
       // Get the response
    
        var reader = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
    
        var buffer = new java.lang.StringBuffer();
    
        var line = "";
    
        while ((line = reader.readLine()) != null) {
    
            buffer.append(line);
    
        }
    
        reader.close();
    
    
    
       // Create array from response
    
        var jsonarray = eval('(' + buffer + ')');
    
    
    
       // Get filenames and titles from Domino database collection resource
    
       // On XPage, requestScope.status is bound to a multi-line text control
    
        for (var i = 0; i < jsonarray.length; i++) {
    
            requestScope.status += jsonarray [i].@filepath + " - " + jsonarray [i].@title + "\n";
    
        }
    
    
    
    } else {// if connection fails
    
        requestScope.status = conn.getResponseCode() + " " + conn.getResponseMessage();
    
    }  
    &nbsp;
    
    &nbsp;
    

    Thank you,

    • conn.setRequestProperty("Authorization"

      By Jeffrey M Winkler 1 decade ago
      I did find one interesting this&nbsp; the HttpURLConnections was not authenticating me. I had to add the following:
      
      &nbsp;
      
      var auth=&quot;&lt;user&gt;:&lt;pass&gt;&quot;
      
      conn.setRequestProperty(&quot;Authorization&quot;,&quot;Basic &quot; + new sun.misc.BASE64Encoder().encode(auth.getBytes()));
      
      conn.connect();
      
      &nbsp;
      
      and then everything works fine.&nbsp; Why do I have to do this?
      
      &nbsp;
      
      Thank you.
      
      • Web server must not allow anonymous access ...

        By Dave Delay 1 decade ago

        The data service depends on the Domino web engine to authenticate the request.  Your original post said you are sending a GET request to http://{host}/api/data.  If this doesn't work without the Authorization header, it probably means your Domino web server doesn't allow anonymous access. 

         

        I forget exactly where anonymous access is configured (and I'm not at work right now).  Check the settings in the server document.  If you are using internet sites to configure the web server, check the web site document.

         

        The sample code must have been executed against a server that allows anonymous access.

         

        -- Dave Delay

        • User is already logged in

          By Jeffrey M Winkler 1 decade ago

          The server is to allow anonymous access, but the application requires a user to log in.  So when the user get to the xPage they have already authenticated. 

          Also, instead of using conn.setRequestProperty("Authorization","Basic "...

          I am getting the var ltpaToken =  facesContext.getExternalContext().getRequestCookieMap().get("LtpaToken")

          and setting conn.setRequestProperty("Cookie","LtpaToken="+ltpaToken )

          This seems to work as well. 

           

          • There may be a simpler approach than using the data service ...

            By Dave Delay 1 decade ago

            It sounds like you are sending requests to the data service for the same database your XPage lives in.  In other words, your XPage lives in x.nsf and you are sending a request to something like http://{host}/x.nsf/api/data/collections.  This might work, but as you have already discovered, your server-side JavaScript code has to add authentication credentials even though the user is already logged in.

             

            The data service was really designed to be used outside the context of XPages.  For example, it can be used from a native mobile application or from a server-side script (PHP, Ruby, etc.) on a foreign server.  If you want to access application data from an XPage, the REST services control might be a better choice.

             

            -- Dave

            • the REST services control

              By Jeffrey M Winkler 1 decade ago

              Dave,

              Actually, I am getting data from multiple databases.

              How do you get a handle to the JSON data in the the REST services control?  I do not want to use the dojoGrid control because I need to display the data with multiple rows for 1 record with data coming from different databases. 

              Thank you,

              Jeff

              • Access the data from the REST Control using the pathInfo property.

                By Stephen Auriemma 1 decade ago

                Jeff,

                The data from the rest service control can be accessed directly using the pathInfo property. You set the pathInfo property of the REST control and then reference in URL of the request:

                URL: http:///XPagesExt.nsf/REST_DataService.xsp/viewEntriesPathInfo

                I suggest you look at the sample XPage Ext Lib sample page Data Service which can be accessed from the REST tab (XPagesExt.nsf/REST_DataService.xsp).

                Steve