Mam wymóg. Mam działającą rozmowę POST ("http://localhost:8080/POSTAPI/table/testmaster/create")
. Wysłałem dane JSON
za pośrednictwem listonosza, a dane zostały wstawione do bazy danych MySQL. Teraz próbuję wysłać dane json
przez apache httpcleint
. ale nie udało się wstawić do bazy danych mysql
.Jak wysłać dane JSON jako ciało w żądaniu POST przez HttpClient
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:8080/POSTAPI/table/testmaster/create");
JSONObject testmaster = new JSONObject();
testmaster.put("testRunId", testRunId);
testmaster.put("testClassName", className);
testmaster.put("testMethod", methodName);
testmaster.put("createdBy", "leela");
testmaster.put("createdDate", startDate);
testmaster.put("lastUpdatedBy", "raghu");
testmaster.put("lastUpdatedDate", endDate);
testmaster.put("attribute1", "methodName");
testmaster.put("attribute1Value",methodName);
testmaster.put("attribute2", "result");
testmaster.put("attribute2Value", successResult);
testmaster.put("attribute3", "Test Suite");
testmaster.put("attribute3Value", suiteName);
testmaster.put("attribute4", "test group");
testmaster.put("attribute4Value", TestGroup);
testmaster.put("attribute5", "dataprovider");
testmaster.put("attribute5Value", dataProvider);
StringEntity stringEntity = new StringEntity(testmaster.toString());
post.setEntity(stringEntity);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(post);
System.out.println("Status: "+response.getStatusLine());
Oto, co próbowałem. jeśli ktokolwiek ma jakiś pomysł na operację po httpclient lub jakąkolwiek inną alternatywę, proszę dać mi znać. Z góry dziękuję.
Możliwy duplikat [Jak POST żądanie JSON przy użyciu Apache HttpClient?] (Http://stackoverflow.com/questions/12059278/how-to-post-json-request-using-apache-httpclient) – Set
To się nie udało. – krish