0

I've setup a RestResource that receives a webhook from Mailchimp. When setting up the webhook in Mailchimp it verifies the webhook URL exists by calling it with a GET Method but then sends all future requests as POST Methods.

How can I setup my rest resource to handle a Get and Post on the same method while avoiding the Method already defined error?

@RestResource(urlMapping='/mailchimp/*')
global without sharing class RestMailchimpWebhook {

    @HttpPost
    global static void callbacks(){
       ... // handle webhooks
    }

    @HttpGet
    global static void callbacks(){
       ... //handle verification
    }
}

1 Answer 1

1

You can't have two methods with the exact same names and parameters in the same class. Just change the names, you'll be fine. The general convention I've seen is doX, where X is the HTTP verb.

@HttpPost
global static void doPost(){
   ... // handle webhooks
}

@HttpGet
global static void doGet(){
   ... //handle verification
}

You can, however, choose whatever name you want to that's a valid method name.

The name of the method has nothing to do with the wildcard in the urlMapping attribute in @RestResource. You would want to specify the URL you're trying to handle in the urlMapping property itself.

@RestResource(urlMapping='/mailchimp/callbacks')
2
  • 1
    Ahh, the issue was I've been using the urlMapping with a star. But by changing it to @RestResource(urlMapping='/mailchimp/callbacks') it will automatically route to doGet and doPost. Perhaps update the answer to include @RestResource for clarity and I'll accept. Thanks!!
    – Dan
    Sep 27, 2022 at 3:03
  • @Dan Sure thing.
    – sfdcfox
    Sep 27, 2022 at 3:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .