3 Replies Latest reply on Jun 25, 2014 12:50 PM by blankenshipr

    Could anyone provide an example of using REST in HTML

    jmcsparin

      I am trying to learn REST and would like to start with a simple html form that makes a GET request for some request types.  I can't seem to find an example that shows the syntax for the full HTML code that embeds the API key and then makes the call.

       

      I have tried examples in the Documentation just using curl such as

      curl "http://localhost/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets?list=group&page=1&limit=3&apiKey=myapikeyhere"

      But it returns with the message "Authentication Required"

       

      If I change it to use

      curl "http://localhost/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets?list=group&page=1&limit=3?username=myusername&password=mypassword"

      It seems to work but I get a massive page of errors starting with {"stackTrace":"java.lang.NullPointerException \r\n\tat

       

      Thanks,

      Joe

        • Re: Could anyone provide an example of using REST in HTML
          blankenshipr

          Here is some sample PHP code that I wrote to create a dashboard of open tickets. It shows how to add the authentication into the request and should help you get started. Just modify lines 2-5 to match your setup, and then start at line 28 to start getting data. The curl_get function is just a wrapper function to do a bunch of stuff with cURL options.

           

          <?php
          $username = "yourUsername";
          $key = "yourSystemApiKey";
          $auth = "username=$username&apiKey=$key";
          $instance = "http://hostname/helpdesk/WebObjects/Helpdesk.woa";
          
          
          function curl_get($url,$opt=null) {
                  global $instance, $auth;
                  $curl_url = $instance . $url;
                  $curl_url .=  (preg_match("/\\?/",$curl_url)) ? "&" : "?";
                  $curl_url .= $auth;
          
                  $opts = array(
                          CURLOPT_URL => $curl_url,
                          CURLOPT_HEADER => 0,
                          CURLOPT_RETURNTRANSFER => 1
                  );
                  if (!is_null($opt)&&is_array($opt)) {
                          $opts = $opt + $opts;
                  }
                  $ch = curl_init();
                  curl_setopt_array($ch,$opts);
                  $result = curl_exec($ch);
                  curl_close($ch);
                  return $result;
          }
          $group_tickets_json = curl_get("/ra/Tickets/group?style=long");
          $group_tickets = json_decode($group_tickets_json);
          echo "<h1>Group Tickets: " . count($group_tickets) . "</h1>";
          echo "<table style=\"width:100%;border-collapse:collapse;border:1px solid #000000\">";
          echo "<tr><th>Num</th><th>Subject</th><th>Client</th><th>Tech</th></tr>";
          foreach ($group_tickets as $ticket) {
                  echo "<tr><td>{$ticket->id}</td><td>{$ticket->subject}</td><td>{$ticket->displayClient}</td><td>{$ticket->clientTech->displayName}</td></tr>";
          }
          echo "</table>";
          
          
          1 of 1 people found this helpful