TECNOLOBO

No recuerdas tu codigo?
Se te olvido como se hace?

Aqui podras guardar lo que necesiten
Y cuando sea necesesario

Creado por julian gomez
iiiiii

consumir webservice con oracle PLSQL



Descripcion

Consumir webservicr con oracle apex haciendo uso d

php


-- recurso para hacer peticiones "https"  : 
--https://oracle-base.com/articles/misc/utl_http-and-ssl
 
declare
    --manejo de json, obj, varchar
    v_json apex_json.t_values;
    
    
    -- manejo del webservice
    req utl_http.req;
    res utl_http.resp;
    url varchar2(4000) := 'http://172.1.1.1:8080/nombre_metodo';
    buffer varchar2(4000); 
    content varchar2(4000) := '{"usuario":"nombreusuario", "clave":"clave", "api_key":"xyz", "secret": "abc"}';
    
    --manejo de respuesta
    v_response VARCHAR2 (32767) := NULL;
    
    --autenticacion con ldap
    v_successful boolean := false;
    
begin
  
  --Set headers
  req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
  utl_http.set_header(req, 'user-agent', 'mozilla/4.0'); 
  utl_http.set_header(req, 'content-type', 'application/json'); 
  utl_http.set_header(req, 'Content-Length', length(content));
 
  --wtite petition to webservice
  utl_http.write_text(req, content);
  
  --set rest with response of the webservice
  res := utl_http.get_response(req);
  
  -- process the response from the HTTP call
  begin
  
    /*Se lee cada linea(buffer) de la respuesta y se almacena en l_json_doc */
    loop
      utl_http.read_line(res, buffer);
      v_response := v_response || buffer;
    end loop;
    utl_http.end_response(res);
    
    
  exception
    when utl_http.end_of_body 
    then
      utl_http.end_response(res);
      
  end;
  -- End process the response from the HTTP call.
   
   begin
   --parseo del varchar tipo json que devuelve el servidor a un objeto de tipo JSON en la variable v_json
   apex_json.parse ( p_values => v_json, p_source => v_response );
  
   if apex_json.get_number(p_values => v_json, p_path   => 'respuesta') = 1 then
       v_successful := true;
       :P2_RESPUESTA := 'Login exitoso';
       --return v_successful;
       
   else
       
       --return v_successful = false;
       :P2_RESPUESTA := 'Login  NO exitoso';
   
   end if;
   
   exception
       when others then
       --return v_successful;
       :P2_RESPUESTA := 'Login  NO exitoso';
   
   end;
   
  
end;
/********************************consumir servicio con HTTPS, parametros por URL y Autorizacion**************************************/

DECLARE
  req   UTL_HTTP.REQ;
  resp  UTL_HTTP.RESP;
  value VARCHAR2(1024);  -- URL to post to
  v_url VARCHAR2(200) := 'https://xxxxxxx.xxxxxx.net/oauth/token';
  -- Post Parameters
  v_param VARCHAR2(500) := 'username=apiuseradminprba'||chr(38)||'password=Eficacia_ADMINprba.83838465'||chr(38)||'grant_type=password';
  v_param_length NUMBER := length(v_param);
BEGIN
	
	-- se llama la billetera antes de consumir el servicio
    UTL_HTTP.set_wallet('file:/u01/app/admin/MISCOLPR/wallet', null);


    req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST');
    
    DBMS_OUTPUT.PUT_LINE(v_param);
  
    UTL_HTTP.SET_HEADER (r      =>  req,
                       name   =>  'Content-Type',
                       value  =>  'application/x-www-form-urlencoded');

    UTL_HTTP.SET_HEADER (r      =>   req,
                       name   =>   'Authorization',
                       value  =>   'Basic V3NJbnRlZ3JhY2lvblByYmE6cHJ1ZWJhcy4xMjM=');
                       
                       
    UTL_HTTP.SET_HEADER (r      =>   req,
                       name   =>   'Content-Length',
                       value  =>   v_param_length);
                                              
    UTL_HTTP.WRITE_TEXT (r      =>   req,
                       data   =>   v_param);  
                       
   resp := UTL_HTTP.GET_RESPONSE(req);
                       
  LOOP
    UTL_HTTP.READ_LINE(resp, value, TRUE);
    DBMS_OUTPUT.PUT_LINE(value);
  END LOOP;
  UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
  WHEN UTL_HTTP.END_OF_BODY THEN
    UTL_HTTP.END_RESPONSE(resp);
END;

/*NOTA: 
para codificar el password de la autorizacion  se debe usar la siguiente liena 
select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_i18n.string_to_raw('usuario:password', 'UTF8'))) from dual;

*/