0

What is the Java Equivalent Post method code for the curl?

curl -X POST https://api.twilio.com/2010-04- 
01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages.json \
--data-urlencode "From=+15017122661" \
--data-urlencode "Body=body" \
--data-urlencode "To=+15558675310" \
-u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token

This is my code:

String message = "Body=" + "This is your message";
String sender = "&From=" + "+14708657743";
String numbers = "&To=" + "+919943666843";
String auth = "ACb3739c0a4bf6d2cc5495a8ff3c545ea9:9ae02782844f6349d24a36bf922746b2";
auth =  Base64.getEncoder().encodeToString(auth.getBytes());
String data =  message + sender + numbers+ auth;
URL url = new URL("https://api.twilio.com/2010-04-01/Accounts/ACb3739c0a4bf6d2cc5495a8ff3c545ea9/Messages.json?"+URLEncoder.encode(data,"UTF-8"));
System.out.println(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + auth);

Where did I go wrong? Can anyone suggest the correct code?

tobsob
  • 602
  • 9
  • 22
  • 2
    Possible duplicate of [How to send cURL -X POST --data-urlencode with Java](https://stackoverflow.com/questions/34690492/how-to-send-curl-x-post-data-urlencode-with-java) –  Sep 06 '19 at 12:31
  • the curl call does something called urlencode which you don't do in your java code. I suggest to look for something like that. – f1sh Sep 06 '19 at 12:31
  • I have called the URL encoder for that reason only. – Rohith Kumar Sep 06 '19 at 12:43

0 Answers0