proc ds2 ; ds2_options sas; /* Note: The HTTP package currently uses the tkhttp extension, not the more recent and improved tkhttpc extension. */ package tap_RESTCallout /overwrite=yes; dcl package tap_logger mLogr(); dcl package http mHclnt(); dcl nvarchar(16384) mURL; method tap_RESTCallout(); mURL = ' '; end; /*----------------------------------------------------------------*/ method createPost( nvarchar(16384) url, int timeout, /* in milliseconds */ nvarchar(256) userid, nvarchar(256) password, in_out int rc ); dcl varchar(16384) hdrVal; hdrVal = 'application/json; charset=utf-8'; /* NOTE: HTTP pkg does not currently accept user/passwd args. */ createPost( url, hdrVal, hdrVal, timeout, userid, password, rc); end; /*----------------------------------------------------------------*/ method createPost( nvarchar(16384) url, nvarchar(16384) contentType, nvarchar(16384) accept, int timeout, /* in milliseconds */ nvarchar(256) userid, nvarchar(256) password, in_out int rc ); /* NOTE: D2HTTP does not yet support credentials. */ if ( missing( url ) ) then do; rc = 1; mLogr.err( 'createPost method requires a URL argument.' ); return; end; rc = 0; if ( url ^= mURL ) then do; mURL = url; if ( mLogr.isDebugEnabled() ) then mLogr.debug( 'Creating POST request for ' || url ); rc = mHclnt.createPostMethod( url ); if ( not rc ) then if ( null( contentType ) ) then rc = mHclnt.setRequestContentType( 'application/json; charset=utf-8' ); else rc = mHclnt.setRequestContentType( contentType ); if ( not rc ) then if ( null( Accept ) ) then rc = mHclnt.addRequestHeader( 'Accept', 'application/json; charset=utf-8' ); else rc = mHclnt.addRequestHeader( 'Accept', Accept ); if ( not rc ) then rc = mHclnt.setSocketTimeout( timeout ); end; end; /*----------------------------------------------------------------*/ method executePost( nvarchar(67108864) payload, in_out int hstat, in_out int rc, in_out nvarchar respBody ); rc = 0; if ( mLogr.isTraceEnabled() ) then mLogr.trace( 'executePost given payload: ' || payload ); mHclnt.setRequestBodyAsString( payload ); mHclnt.executeMethod(); hstat = mHclnt.getStatusCode(); if ( mLogr.isTraceEnabled() ) then do; dcl nvarchar(4096) respHdrs; mHclnt.getResponseHeadersAsString( respHdrs, rc ); if ( not rc ) then do; mLogr.trace( '--- Beginning of response headers ---'); mLogr.trace( respHdrs ); mLogr.trace( '--- End of response headers ---------'); end; end; mHclnt.getResponseBodyAsString( respBody, rc ); if ( rc ) then mLogr.err( 'getResponseBodyAsString rc = ' || rc ); end; /*----------------------------------------------------------------*/ method executePost( nvarchar(67108864) payload, in_out int hstat, in_out int rc, in_out nvarchar respBody, nvarchar(16384) url, int timeout, nvarchar(256) userid, nvarchar(256) password ); hstat = 0; rc = 0; /* NOTE: HTTP pkg does not currently accept user/passwd args. */ createPost( url, timeout, userid, password, rc ); if ( not rc ) then executePost( payload, hstat, rc, respBody ); end; /*----------------------------------------------------------------*/ method executePost( nvarchar(67108864) payload, in_out int hstat, in_out int rc, in_out nvarchar respBody, nvarchar(16384) url ); hstat = 0; rc = 0; createPost( url, 0, null, null, rc ); if ( not rc ) then executePost( payload, hstat, rc, respBody ); end; /*----------------------------------------------------------------*/ endpackage; run; quit;