Ho un requisito. Ho una chiamata POST di lavoro ("http://localhost:8080/POSTAPI/table/testmaster/create")
. Ho inviato i dati JSON
tramite postino e i dettagli sono stati inseriti nel database MySQL. Ora sto cercando di inviare i dati json
tramite apache httpcleint
. ma, non è riuscito a inserire nel database mysql
.Come inviare dati JSON come corpo nella richiesta POST tramite 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());
Questo è quello che ho provato. se qualcuno ha qualche idea di operazione post attraverso httpclient o qualsiasi altra alternativa per favore fatemelo sapere. Grazie in anticipo.
Eventuali duplicati di [Come Inserisci richiesta JSON utilizzando Apache HttpClient?] (Http://stackoverflow.com/questions/12059278/how-to-post-json-request-using-apache-httpclient) – Set
Quello non ha funzionato in realtà. – krish