0

I am trying to do a POST request using Volley, below is the kind of request i'd like to make, a header and a JSON body

I've tried using Unirest api (Unirest.io) but it requires min sdk as 24, so i had to switch to Volley

HttpResponse<String> response = Unirest.post("https://api.msg91.com/api/v2/sendsms?country=91")
  .header("authkey", "")
  .header("content-type", "application/json")
  .body("{ \"sender\": \"SOCKET\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"Message1\", \"to\": [ \"98260XXXXX\", \"98261XXXXX\" ] }, { \"message\": \"Message2\", \"to\": [ \"98260XXXXX\", \"98261XXXXX\" ] } ] }")
  .asString();

1 Answers1

0

Try with OkHttp, following is a simple

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(JSON, json /* your json string*/);
Request request = new Request.Builder()
  .url("https://api.msg91.com/api/v2/sendsms?country=91")
  .post(body)
  .addHeader("authkey", "Token")
  .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW") 
  .build();

Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody=response.body().string()
}


You can add any header, add your json string as a body

Abdul
  • 869
  • 6
  • 14