I'm doing an App on Android and I need to connect to an API hosted on localhost.
/** Creating Connection **/
URL serverAddress = new URL(link);
HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "Mozilla/5.0");
connection.setRequestMethod("POST");
// JSON
JSONObject jsonObject = new JSONObject();
jsonObject.put("number", number);
/** POSTing **/
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(jsonObject.toString());
writer.flush();
writer.close();
os.close();
connection.connect();
Log.e("AdvancedNotifications", "sending " + jsonObject.toString() + " to " + link);
Log.e("AdvancedNotifications", "response code: " + String.valueOf(connection.getResponseCode()));
Log.e("AdvancedNotifications", "response msg: " + connection.getResponseMessage());
And it gives me error 400 (Bad request).
I've tried Python with requests and it works.
When testing the request from Python by having it print the POST in the console, it gives me this:
192.168.0.158 - - [25/Oct/2018 14:10:35] "POST /call HTTP/1.1" 200 -
{'number': '1642555315'}
And doing it with the request from the App gives me this:
192.168.0.158 - - [25/Oct/2018 14:11:40] "POST /call HTTP/1.1" 400 -
EDIT: When making the request with CURL, I get this error:
{"message": {"number": "Missing required parameter in the post body"}}