1 Reply Latest reply on May 31, 2017 3:25 PM by Mark Sta Ana

    Correct usage of htp in a GET handler

    Mark Sta Ana

      Folks,

       

      I have the following PL/SQL block as my GET handler:

       

      begin
          owa_util.mime_header('application/json', false, 'ISO-8859-4');
          htp.p('{"hello":"world"}');
      end;
      

       

      When, I try to get the data, there's no body. However upon closer inspection I can see the data in the header:

       

      c:\> curl --verbose http://127.0.0.1:8080/ords/my_schema/foo/bar
      * timeout on name lookup is not supported
      *   Trying 127.0.0.1...
      * TCP_NODELAY set
      * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
      > GET /ords/my_schema/foo/bar HTTP/1.1
      > Host: 127.0.0.1:8080
      > User-Agent: curl/7.52.1
      > Accept: */*
      >
      < HTTP/1.1 200 OK
      < Content-Type: application/json; charset=ISO-8859-4
      < {"hello": "world"}
      < ETag: "ZBFj9qz1a1G6scu6ZH7GH/7aT68XXwqgtwd2ToVnKouIAZsrerIv180AVjFuSJqLrn9K4z2vha7/Ll9mNGZoxw=="
      < Transfer-Encoding: chunked
      <
      * Curl_http_done: called premature == 0
      * Connection #0 to host 127.0.0.1 left intact
      

       

      How I can turn the {"hello": "world"} into the body of the response?

       

      To under the problem compare this to a plain ole GET using a query (source type collection): select 'world' as "hello" from dual

       

      c:\> curl --verbose http://127.0.0.1:8080/ords/my_schema/foo/baz
      * timeout on name lookup is not supported
      *   Trying 127.0.0.1...
      * TCP_NODELAY set
      * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
      > GET /ords/my_schema/foo/baz HTTP/1.1
      > Host: 127.0.0.1:8080
      > User-Agent: curl/7.52.1
      > Accept: */*
      >
      < HTTP/1.1 200 OK
      < Content-Type: application/json
      < ETag: "hnT3Dyo3ZCnRHjO0XjISTIZxO33Ms/ovn3GKg+Nvx1sZh+yTSJUKK7vt8if3eZyTiUjm0KqtM9WrKfMcZoHlHw=="
      < Transfer-Encoding: chunked
      <
      {"items":[{"hello":"world"}],"hasMore":false,"limit":25,"offset":0,"count":1,"links":[{"rel":"self",
      "href":"http://127.0.0.1:8080/ords/swftrefadmin/foo/baz"},{"rel":"describedby","href":"http://127.0.
      0.1:8080/ords/swftrefadmin/metadata-catalog/foo/item"},{"rel":"first","href":"http://127.0.0.1:8080/
      ords/swftrefadmin/foo/baz"}]}* Curl_http_done: called premature == 0
      * Connection #0 to host 127.0.0.1 left intact
      
        • 1. Re: Correct usage of htp in a GET handler
          Mark Sta Ana

          Confession time...

           

          The reason I went down the htp route was that I couldn't get the following pl/sql block to work in a GET handler:

           

          DECLARE
              l_cursor   SYS_REFCURSOR;
          BEGIN
              apex_json.initialize_clob_output;
              pck_reporting.emp_sp(l_cursor);
              apex_json.open_object;
              apex_json.write('emp', l_cursor);
              apex_json.close_object;
              apex_json.free_output;
          END;
          /
          

           

          This was returning HTTP 200 (OK) status, but no data.

           

          I found the answer in Tim Hall's excellent ORDS guide, if you remove call to apex_json.initialize_clob_output everything begins to work!

           

          For completeness sake here's what the pl/sql block should look like in the GET handler:

           

          DECLARE
              l_cursor   SYS_REFCURSOR;
          BEGIN
              pck_reporting.emp_sp(l_cursor);
              apex_json.open_object;
              apex_json.write('emp', l_cursor);
              apex_json.close_object;    
          END;
          /