How to execute webservice call from the custom rule without provisioning plan

Hi Everyone,

i am trying to call post api operation from the custom code without provisioning plan, by using the below code, but i am getting the error for Priviliged exception.

   import sailpoint.connector.webservices.EndPoint;
   import sailpoint.connector.webservices.WebServicesClient;
   EndPoint requestEndPoint = new EndPoint();
   WebServicesClient restClient = new WebServicesClient();
   Map jsonMap = new HashMap();
   List userCurrentRoles = new ArrayList();
   if (userCurrentRoles != null & amp; & amp; !userCurrentRoles.isEmpty()) {
       jsonMap.put("roles", userCurrentRoles);
   }
   String finalbody = JsonUtil.render(jsonMap);
   EndPoint requestEndPoint = new EndPoint();
   WebServicesClient restClient = new WebServicesClient();
   Map headers = new HashMap();
   headers.put("Content-Type", "application/json");
   List allowedStatus = new ArrayList();
   allowedStatus.add("200");
   String fullUrl = "";
   restClient.executePost(fullUrl, finalbody, headers, allowedStatus);

please help me how can i call the api by using the sailpoint libraries.

thanks in advance

Hey, something like this should work for you.

import org.json.JSONObject;
import java.io.OutputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

private void makePostCall(){
	
	//set up the requirements for making API calls
    List<String> allowedStatuses = new ArrayList();
    allowedStatuses.add( "200" );
	Map header = requestEndPoint.getHeader();

    // Your request body if it is in json format. 
    List userCurrentRoles = new ArrayList();
    Map jsonMap = new HashMap();
    if(userCurrentRoles != null && !userCurrentRoles.isEmpty()){
        jsonMap.put(“roles”, userCurrentRoles);
    }

	try {
		//executeGet operates in the context of the source restClient.  
		//will follow Authentication on the source/endpoint.
		//String dataAccesses = restClient.executeGet(dataAccessUrl, header, allowedStatuses );
		
        URL requestUrl = new URL(requestEndPoint.getFullUrl());
	
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Authorization", header.get("Authorization"));

        JSONObject jsonBodyMap = new JSONObject( jsonMap );

        OutputStream os = connection.getOutputStream();
        os.write( jsonBodyMap.toString().getBytes() );
        os.flush();
        os.close();

        if ( connection.getResponseCode() != 201) {
            log.error(logPrefix + "********ERROR AFTER RULE: " + connection.getResponseCode() );
        }
    } catch ( Exception e ) {
        log.error(logPrefix + "********ERROR AFTER RULE: " + e.getMessage(), e );
    }

}

Hey, when I use this script, I am getting the following exception:

Cannot access method setRequestMethod(java.lang.String) in ‘class sun.net.www.protocol.https.HttpsURLConnectionImpl’

What a I doing wrong?

Make sure you are importing HttpUrlConnection class
import java.net.HttpURLConnection;
instead of
import sun.net.www.protocol.http.HttpURLConnection;

Yes ofcourse, that is what I imported. But is that still applicable if my url is an https url?

can you send the full code what you are trying

Yes it is still applicable.

URL requestUrl = new URL( "https://testUrl.com" );
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();

If its still giving the same error please share your code, it would help to find your issue easily.

Here is my code which I copied from this post:

String url = "https://xxx.api.identitynow.com/v3/"+transformId;
	String parsedJson = jsonPayload.replace("\"","\\\"");
  try {

	URL requestUrl = new URL(url);

	HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
	connection.setRequestMethod("PUT");
	connection.setDoOutput(true);
	connection.setRequestProperty("Content-Type", "application/json");
	connection.setRequestProperty("Authorization","Bearer "+accessToken);

	//JSONObject jsonBodyMap = new JSONObject( jsonMap );

	OutputStream os = connection.getOutputStream();
	os.write(parsedJson.getBytes());
	os.flush();
	os.close();

	if ( connection.getResponseCode() != 200 ||  connection.getResponseCode() != 201) {
	  log.error(logPrefix + "********ERROR AFTER RULE: " + connection.getResponseCode() );
	}
  } catch ( Exception e ) {
	log.error(logPrefix + "********ERROR AFTER RULE: " + e.getMessage(), e );
  }

The issue seems to be of importing class from other library. I would suggest you to try something like this make sure you’re using correct class.

java.net.URL requestUrl = new java.net.URL(url);

java.net.HttpURLConnection connection = (java.net.HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
1 Like