Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request send JSON data Java HttpUrlConnection

I have developed a Java code that convert the following cURL to java code using URL and HttpUrlConnection. the cURL is :

curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}' 

I have written this code but it always gives HTTP code 400 bad request. I couldn't find what is missing.

String url="http://url.com"; URL object=new URL(url);  HttpURLConnection con = (HttpURLConnection) object.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setRequestMethod("POST");  JSONObject cred   = new JSONObject(); JSONObject auth   = new JSONObject(); JSONObject parent = new JSONObject();  cred.put("username","adm"); cred.put("password", "pwd");  auth.put("tenantName", "adm"); auth.put("passwordCredentials", cred.toString());  parent.put("auth", auth.toString());  OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString()); wr.flush();  //display what returns the POST request  StringBuilder sb = new StringBuilder();   int HttpResult = con.getResponseCode();  if (HttpResult == HttpURLConnection.HTTP_OK) {     BufferedReader br = new BufferedReader(             new InputStreamReader(con.getInputStream(), "utf-8"));     String line = null;       while ((line = br.readLine()) != null) {           sb.append(line + "\n");       }     br.close();     System.out.println("" + sb.toString());   } else {     System.out.println(con.getResponseMessage());   }   
like image 440
user3244172 Avatar asked Jan 28 '14 11:01

user3244172


1 Answers

Your JSON is not correct. Instead of

JSONObject cred = new JSONObject(); JSONObject auth=new JSONObject(); JSONObject parent=new JSONObject(); cred.put("username","adm"); cred.put("password", "pwd"); auth.put("tenantName", "adm"); auth.put("passwordCredentials", cred.toString()); // <-- toString() parent.put("auth", auth.toString());              // <-- toString()  OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString()); 

write

JSONObject cred = new JSONObject(); JSONObject auth=new JSONObject(); JSONObject parent=new JSONObject(); cred.put("username","adm"); cred.put("password", "pwd"); auth.put("tenantName", "adm"); auth.put("passwordCredentials", cred); parent.put("auth", auth);  OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString()); 

So, the JSONObject.toString() should be called only once for the outer object.

Another thing (most probably not your problem, but I'd like to mention it):

To be sure not to run into encoding problems, you should specify the encoding, if it is not UTF-8:

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); con.setRequestProperty("Accept", "application/json");  // ...  OutputStream os = con.getOutputStream(); os.write(parent.toString().getBytes("UTF-8")); os.close(); 
like image 93
hgoebl Avatar answered Sep 20 '22 13:09

hgoebl