Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException raised when attempting to create a campaign when API key is not set. #11

Open
jdennes opened this issue Jan 28, 2013 · 2 comments

Comments

@jdennes
Copy link
Contributor

jdennes commented Jan 28, 2013

The following code:

Campaigns campaigns = new Campaigns();
CampaignForCreation c = new CampaignForCreation();
c.Name = "Example";
c.Subject = "Example";
c.FromName = "Example";
c.FromEmail = "[email protected]";
c.ReplyTo = "[email protected]";
c.HtmlUrl = URI.create("http://example.com/content.html");
c.ListIDs = new String[] { "listid" };
c.SegmentIDs = new String[] { "" };
String id = campaigns.create("clientid", c);
System.out.printf("Campaign created with ID: %s", id);

Results in:

Exception in thread "main" java.lang.NullPointerException
    at com.createsend.util.JerseyClientImpl.handleErrorResponse(JerseyClientImpl.java:363)
    at com.createsend.util.JerseyClientImpl.post(JerseyClientImpl.java:207)
    at com.createsend.util.JerseyClientImpl.post(JerseyClientImpl.java:195)
    at com.createsend.Campaigns.create(Campaigns.java:106)
    at Tester.main(Tester.java:24)

What should actually happen is this:

Exception in thread "main" com.createsend.util.exceptions.UnauthorisedException: The CreateSend API responded with the following authentication error 50: Must supply a valid HTTP Basic Authorization header
    at com.createsend.util.JerseyClientImpl.handleErrorResponse(JerseyClientImpl.java:363)
    at com.createsend.util.JerseyClientImpl.get(JerseyClientImpl.java:148)
    at com.createsend.util.JerseyClientImpl.get(JerseyClientImpl.java:133)
    at com.createsend.util.JerseyClientImpl.get(JerseyClientImpl.java:107)
    at com.createsend.General.getClients(General.java:88)
    at Tester.main(Tester.java:31)
@ghost ghost assigned jdennes Jan 28, 2013
@jdennes
Copy link
Contributor Author

jdennes commented Jan 28, 2013

The core of the problem here appears to be a problem with Jersey not including the body of the response when a 401 Unauthorized response is received after issuing a POST request (so it's not specific to creating a campaign). GET requests which respond with a 401 Unauthorized appear to be fine.

This can be shown by trying the following.

If you change the com.createsend.util.ErrorDeserialiser.getErrorResponse() method as follows:

public ApiErrorResponse<T> getResponse(ClientResponse response) {
    ParameterizedType type = JerseyClientImpl.getGenericReturnType(this.getClass(), 2);
    String body = response.getEntity(String.class);
    return response.getEntity(new GenericType<ApiErrorResponse<T>>(type));
}

The body variable representing the body of the response will be as expected for GET requests which respond with a 401 Unauthorized, but it will be empty for POST requests which respond with a 401 Unauthorized.

/cc @tobio (any experience with Jersey when originally implementing this library would be appreciated)

@tobio
Copy link
Contributor

tobio commented Jan 29, 2013

@jdennes, I didn't run into anything like that. It might be worth checking out the newer versions of Jersey (seems to be up to 1.9) to see if the behaviour's changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants