The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

How to get http status code from response using java sdk

Highlighted
Occasional Contributor

How to get http status code from response using java sdk

Hi ,

 

I am using java sdk for bulk activities. How I can get the http response status code from response returned or from exception thrown.

I could not find a way for it. Can some one help me in this?

 

Exampl code:

 

ConstantContact constantContact = new ConstantContact(key, token);

ExportContactsRequest bulkReq = new ExportContactsRequest();

// set bulkReq details

try {

ContactsResponse res = constantContact.exportBulkContacts(bulkReq);

 

}

catch (ConstantContactServiceException e)
{  

 e.printStackTrace();

}

 

Thanks,

Ravi

3 REPLIES 3
Highlighted
Honored Contributor

Re: How to get http status code from response using java sdk

Hello Ravi,

 

You can get this data using the getErrorInfo() method of the ConstantContactServiceException class. It will return List<CUrlRequestError>. Each CUrlRequestError object will have a key/value pair, and one of those pairs will contain the status code for your request. In addition, this information will contain the error message (if any) that is returned by the API.

 

Best Regards,

Elijah G.
API Support Engineer
Highlighted
Occasional Contributor

Re: How to get http status code from response using java sdk

Thanks Elijah,

 

I have tried this thing to get status code like (3XX, 4XX or 5XX) but could not get.

It looks like there are some mappings created for these codes and some error key/value is returned based on that. 

Is there any documentation available which can tell me these details.

 

Thanks,

Ravi

Highlighted
Honored Contributor

Re: How to get http status code from response using java sdk

Hi Ravi,

 

After working on this a bit further, I was able to find an error where the status code information was being omitted from the response info when there was a JSON body present in the response. Given how this is happening, we are looking into how we can fix the problem properly and make the library most usable.

 

However, as I know you are dealing with the problem right now, I have created a "quick and dirty" fix that will enable this functionality in the immediate term while we are looking for a better solution. To make use of this, you will want to update src/main/java/com/constantcontact/util/http/HttpProcessor.java from this Gist: https://gist.github.com/egaiter/59228f03bcc2e98d9fc8

 

Once you've done this, it will force all errored requests that have a JSON body to also include a status_code key/value pair in the request info.

 

If you have any further questions, please let me know!

 

Best Regards,

Elijah G.
API Support Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured