i have a code to request an Database Entry over an API. But i have to send a JSON Object instead of Parameter.
Code is as bellow:
public static void main(String[] args) throws Exception {
URL url = new URL("https://example.com/api/user?method=GetUserData&key=1234567890");
Map<String,Object> params = new LinkedHashMap<>();
params.put("chash","709e8b37182e36092750bebbbf96bc3");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencode");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
}
But, of course it get an Error that the chash is not set.
How can i POST as RAWDATA JSONObject instead of that params.put(...)
Thank you in advice
T-Hope