Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to call API after publishing content

Avatar

Level 2

Hi all,

 

I want to ask how to call an API every time when the users publish the content? 

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @fang_29 

To call an API every time a user publishes content, you can create an OSGi event handler that listens for the com.day.cq.replication.ReplicationAction.EVENT_TOPIC event. This event is fired whenever content is replicated in AEM, which includes when content is published.

Here is sample code you can use and play around with:

 

@Component(service = EventHandler.class, property = {
    EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC
})
public class MyEventHandler implements EventHandler {

  @Reference
  private SlingHttpService slingHttpService;

  @Override
  public void handleEvent(Event event) {
    // Check if the event is a publication event
    if (ReplicationAction.isPublicationEvent(event)) {
      // Get the path of the published content
      String path = ReplicationAction.getPath(event);
      // Call the API
      callAPI(path);
    }
  }

  private void callAPI(String path) {
    // Use the SlingHttpService to make a request to the API
    slingHttpService.doPost("/api/call", null, null, new HtmlResponse() {
      // Implement the onSuccess and onError methods to handle the API response
    });
  }
}

 

https://medium.com/@monendra80/3-ways-to-call-an-api-when-content-is-published-in-aem-b8cbb9b7e8da

 

Hope it helps.

Thanks,

Monendra

View solution in original post

1 Reply

Avatar

Correct answer by
Level 4

Hi @fang_29 

To call an API every time a user publishes content, you can create an OSGi event handler that listens for the com.day.cq.replication.ReplicationAction.EVENT_TOPIC event. This event is fired whenever content is replicated in AEM, which includes when content is published.

Here is sample code you can use and play around with:

 

@Component(service = EventHandler.class, property = {
    EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC
})
public class MyEventHandler implements EventHandler {

  @Reference
  private SlingHttpService slingHttpService;

  @Override
  public void handleEvent(Event event) {
    // Check if the event is a publication event
    if (ReplicationAction.isPublicationEvent(event)) {
      // Get the path of the published content
      String path = ReplicationAction.getPath(event);
      // Call the API
      callAPI(path);
    }
  }

  private void callAPI(String path) {
    // Use the SlingHttpService to make a request to the API
    slingHttpService.doPost("/api/call", null, null, new HtmlResponse() {
      // Implement the onSuccess and onError methods to handle the API response
    });
  }
}

 

https://medium.com/@monendra80/3-ways-to-call-an-api-when-content-is-published-in-aem-b8cbb9b7e8da

 

Hope it helps.

Thanks,

Monendra