This blog post will walk you through the steps to send an SMS over query parameters (over GET method) using Infobip SMS API and Java as the chosen programming language.

So why would we use a GET query method instead of a typical way of sending an SMS using a POST Send SMS call?

There are at least a couple of reasons:

  • You want to form a query string dynamically (on a set of conditions) instead of declaring each parameter separately without forming a more complicated JSON body.
  • GET assumes a different authorization approach (login/pass), which could be preferable to POST (should have IBSSOToken or APIKey or Basic or OAuth2).
  • GET call is simpler in comparison to POST.

Prerequisites

Import okhttp3 dependency

okhttp3 is a HTTP client with straight-forward semantic that allows to do HTTP calls to Infobip API without additional complications.

We’ve included maven and gradle samples for you to choose from depending on which build automation tool you used in your project.

maven

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.10.0</version>
</dependency>

gradle

implementation "com.squareup.okhttp3:okhttp:4.10.0"

Copy an example

This program downloads a URL and returns a response object to process. Just replace necessary placeholders.

Please note that username, password, and to are required parameters.

OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
    .url("https://89.api.infobip.com/sms/1/text/query?username=<LOGIN>&password=<PASS>&from=<SENDER_NAME>&to=<MOBILE_NUMBER>&text=<YOUR_MESSAGE>")
    .method("GET", null)
    .addHeader("Accept", "application/json")
    .build();
Response response = client.newCall(request).execute();

Possible failure messages

401: {"requestError":{"serviceException":{"messageId":"UNAUTHORIZED","text":"Invalid login details"}}}  ← invalid login/pass or user unauthorized to execute remote API
200: {"messages":[{"to":"<MOBILE_NUMBER>","status":{"groupId":5,"groupName":"REJECTED","id":19,"name":"REJECTED_ROUTE_NOT_AVAILABLE","description":"Route not available","action":"Contact account manager"},"messageId":"36635455539401630909","smsCount":1}]}  ← no SMS credits on your Infobip account
200: {"messages":[{"to":"<OTHER_MOBILE_NUMBER>","status":{"groupId":5,"groupName":"REJECTED","id":18,"name":"REJECTED_DESTINATION_NOT_REGISTERED","description":"Destination not registered"},"messageId":"36635482970837212230","smsCount":1}]}  ← mobile phone is in rejected destination/area.

Additional examples

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();
 
  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}