0

I am trying to send email via web service. The curl script which is logged by AhcCurlRequestLogger works fine when I paste it into terminal. But service method not work. The code does not enter wsResponse part.

The java function:

public CompletionStage<String> sendEmail(String to, String subject, String content) {

    JsonNode requestBody = new ObjectMapper().createObjectNode()
            .put("username", this.apiUsername)
            .put("api_key", this.apiKey)
            .put("from", this.mailFrom)
            .put("bcc", this.mailBcc)
            .put("reply_to", this.mailFrom)
            .put("recipient", to)
            .put("subject", subject)
            .put("campaign_name", subject)
            .put("raw_html", content);

    return ws.url(this.apiUrl)
            .setRequestFilter(new AhcCurlRequestLogger())
            .post(requestBody)
            .thenApply((WSResponse wsResponse) -> {
                int responseStatus = wsResponse.getStatus();
                if (responseStatus >= 200 && responseStatus < 300) {
                    log.info("An email with subject " + subject + " was sent successfully to: " + to);
                    return "Mail was sent successfully.";
                } else {
                    log.error("An email with subject " + subject + " could not send successfully to: " + to);
                    return "Mail could not sent successfully.";
                }
            });
} 

The curl script:

  [info] p.l.w.a.AhcCurlRequestLogger - curl \
    --verbose \
    --request POST \
    --header 'Content-Type: application/json' \
    --data '{
    "username" : "secret_username",
    "api_key" : "secret_key",
    "from" : "some@mail.com",
    "bcc" : "some@mail.com",
    "reply_to" : "some@mail.com",
    "recipient" : "some@mail.com",
    "subject" : "top_secret",
    "campaign_name" : "top_secret",
    "raw_html" : "<h3>super mail</h3>"
  }' \
    'https://madmimi.com/api/v3/transactionalEmails'

I have not any error log. I am using playframework 2.7.3 version. I though that ssl verification may cause this. So i added ssl certificate chain to trustStore. It does not work. Then I removed it and add

play.ws.ssl.loose.acceptAnyCertificate = true

to application.conf. It also does not work.

karakale
  • 126
  • 11

1 Answers1

0

The logging in your code snippet will run only if the promise returned by .post(requestBody) completes successfully. Therefore it would seem that there is an exception preventing the promise from completing.

To handle this situation you can add the exceptionally-method:

    return ws.url(this.apiUrl)
        .setRequestFilter(new AhcCurlRequestLogger())
        .post(requestBody)
        .thenApply((WSResponse wsResponse) -> {
            ...
        })
        .exceptionally(throwable -> {
            log.error("The request failed", throwable);
            return "Mail could not be sent successfully.";
        });

When you get the error logged it might be obvious how to fix it. If not, add the error to the question.