Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8079399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:05:18+00:00 2026-06-05T16:05:18+00:00

I am implementing an API call to the MailChimp API in my web application.

  • 0

I am implementing an API call to the MailChimp API in my web application. But this question is valid for any call to an API which accepts a simple HTTP GET.

I would like to catch any errors and allow my application to continue without issues even if the API call failed, The mailchimp servers and down, slow or unavailable.

MailChimp API Returns a serialized JSON Object. This is my code.

def listSubscribe = { apiurl, apikey, listid, email ->
    def url = "${apiurl}?method=listSubscribe&apikey=${apikey}&id=${listid}&email_address=${email}"
}
try {
   def url =  new URL(listSubscribe(apiUrl,apiKey,listId,email))
   return (url.text == 'true') ? true : false
} catch (MalformedURLException e) {
        return false
} catch (java.net.UnknownHostException e) {
        return false
}

Should I perform any other try/catch? How can I improve my code to make it safer for inaccesible API calls?

Solution

In order to make the call asynchronous and since I am using this code inside a Grails application I created a Quartz Job to execute the service containing the API Call.

class MailChimpListSubscribeJob {
    def mailChimpService 

    def execute(context) {
         mailChimpService.listSubscribe(context.mergedJobDataMap.get('email'))       
     }
}

The Service now uses a timeout and catches the generic Exception:

class MailChimpService {
    def grailsApplication

    def listSubscribe(email_address) {
        def apiurl = grailsApplication.config.mailchimp.apiUrl
        def apikey = grailsApplication.config.mailchimp.apiKey
        def listid = grailsApplication.config.mailchimp.listId
        listSubscribe(apiurl, apikey, listid, email_address)
    }

    def listSubscribe(apiurl, apikey, listid, email) {
        try {
            def cmdurl = "${apiurl}?method=listSubscribe&apikey=${apikey}&id=${listid}&email_address=${email}"
            def url =  new URL(cmdurl)
            def response = url.getText(connectTimeout: 4 * 1000, readTimeout: 4  * 1000)
            return (response == 'true') ? true : false  
        } catch (MalformedURLException e) {
                return false
        } catch (java.net.UnknownHostException e) {
            return false
        } catch (Exception e) {
            return false
        }
    }
}

And inside my controllers:

MailChimpListSubscribeJob.triggerNow([email: 'myemail@example.com'])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:05:20+00:00Added an answer on June 5, 2026 at 4:05 pm

    Sergio – You may also want to do the following:

    1. Enable timeout on this call in case you don’t get any response from their system in a few seconds. This will take care of the events when their system is slow to respond, or offline.
    2. If it’s not a critical call that determines whether you can proceed with the rest of the code execution, then you should also consider making it asynchronous.
    3. Catch the generic Exception as well in case you want to handle any unforeseen runtime exceptions that may occur.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to try to use Web API make a rest call but I
I'm implementing an API using authentication based on this article: http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ And this related
I am implementing the Express Checkout API for a web application, and everything works
I want to implement the Wikitude API in my iPhone Application. But while implementing
I'm currently working on implementing the public API of our web application. The application
I'm busy implementing the wurfl api using PHP, but somehow just can't seem to
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I have a UIWebView with content populated from a Last.fm API call. This content
I'm implementing a high traffic client web application that uses a lot of REST
I'm implementing history API for my web app and of course IE doesn't support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.