I have the below code on the Java client Side which is calling a Spring Based REST service,I get a HTTP 400 Error .But it works perfect with the POST man Client
Java Client :
public void getData(String ip){
try{
JSONObject json=new JSONObject();
json.put("time_range", "22-23");
json.put("flow_id", "786");
json.put("ip_a", "192.65.78.22");
json.put("port_a", "8080");
json.put("regex", "%ab");
URL url=new URL("http://"+ip+":8080/pcap");
HttpURLConnection httpcon=(HttpURLConnection)url.openConnection();
httpcon.setDoOutput(true);
httpcon.setRequestMethod("POST");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
OutputStreamWriter output=new OutputStreamWriter(httpcon.getOutputStream());
System.out.println(json);
output.write(json.toString());
httpcon.connect();
String output1=httpcon.getResponseMessage();
System.out.println(output1);
}catch(Exception e){
}
The server side REST Service Code is
@RequestMapping(value = URIConstansts.PCAP, produces = { "application/json" }, method = RequestMethod.POST)
public ResponseEntity getPcap(HttpServletRequest request,@RequestBody PcapParameters pcap_params )
Now when the client sends a request it gives me http 400 error ,and it may be because on client side im sending JSON string while on the server side its a body ,but this works on POST MAN
Can anyone please suggest how can I send a POST request to a spring based REST service via Java