Jose
Jose

Reputation: 1829

MailGun Api Example

I'm doing a test with mailgun, and I copy the example and put my domain and password, but it's not going, and I do not get the mail either

I have put my : Domain Name and API Key.

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public static JsonNode sendSimpleMessage() throws UnirestException {

    HttpResponse<JsonNode> request = Unirest
            .post("https://api.mailgun.net/v3/" + "sandbox21801cd223b94bbc92dbd011424xxxxx.mailgun.org"
                    + "/messages")
            .basicAuth("api", "xxx8013e6a2e75889370b0d2e670b2e")
            .queryString("from", "Excited User <[email protected]>")
            .queryString("to", "[email protected]").queryString("subject", "hello")
            .queryString("text", "testing").asJson();

    return request.getBody();
}

Error:

Exception in thread "main" com.mashape.unirest.http.exceptions.UnirestException: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at character 1
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at com.abalia.crm482.sms_mail.mailSendInfobip.sendSimpleMessage(mailSendInfobip.java:22)
at com.abalia.crm482.sms_mail.mailSendInfobip.main(mailSendInfobip.java:11)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at character 1
at com.mashape.unirest.http.HttpResponse.<init>(HttpResponse.java:106)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:139)
... 3 more

Caused by: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at character 1
at com.mashape.unirest.http.JsonNode.<init>(JsonNode.java:51)
at com.mashape.unirest.http.HttpResponse.<init>(HttpResponse.java:95)
... 4 more
Caused by: org.json.JSONException: A JSONArray text must start with '[' at character 1
at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
at org.json.JSONArray.<init>(JSONArray.java:113)
at org.json.JSONArray.<init>(JSONArray.java:157)
at com.mashape.unirest.http.JsonNode.<init>(JsonNode.java:48)
... 5 more

Upvotes: 0

Views: 1157

Answers (1)

SmartFingers
SmartFingers

Reputation: 105

I had the same issue when I passed in a wrong key. By replacing public key with private key (which you can find from mailgun console), I fixed this problem.

Basically what happened when you pass in a wrong key is like this: If you debug inside into com.mashape.unirest.http.HttpResponse<T>, inside the constructor, you will find how the JsonNode was build. And rawBody, which suppose to be a json string, become a simple String: Forbidden if you failed the key authentication. And trying to build a JsonArray from such a string throws such exception.

Hope it helps~

Upvotes: 0

Related Questions