4

I want to send a JSON object(Note it should not be converted into a string as the server side code is based on the Spring starter project and has params as (@RequestBody PCAP pcap) )I have my below code but it converts the body into a string which gives me 400 bad request .

private void sendData(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");
        Cookie cookie=new Cookie("user", "abc");
        cookie.setValue("store");
        httpcon.setRequestProperty("Accept", "application/json");
        httpcon.setRequestProperty("Cookie", cookie.getValue());

        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){

    }

}

Note: Server side code is

@RequestMapping(value = URIConstansts.PCAP, produces = { "application/json" }, method = RequestMethod.POST)
    public  ResponseEntity getPcap(HttpServletRequest request,@RequestBody PcapParameters pcap_params )
arpit joshi
  • 1,699
  • 7
  • 23
  • 53

1 Answers1

3

I prefer to continue with HttpURLConnection over HttpClient. Some comments over advantages can be found at this SE question

output.write(json.toString());

should be changed to

byte[] jsonBytes = json.getBytes("UTF-8");
output.write(jsonBytes);
output.flush();

Do not forget to call flush() after writing the object and UTF-8 format should be instructed before write operation.

Community
  • 1
  • 1
Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
  • Have a look at same type of question in SE : http://stackoverflow.com/questions/21404252/post-request-send-json-data-java-httpurlconnection – Ravindra babu Sep 25 '15 at 13:54
  • No its not working im not getting the request on server side ,below is server side code @RequestMapping(value = URIConstansts.PCAP, produces = { "application/json" }, method = RequestMethod.POST) public ResponseEntity getPcap(HttpServletRequest request,@RequestBody PcapParameters pcap_params ) – arpit joshi Sep 26 '15 at 10:12
  • Add one more line your code. httpcon.getResponseCode(). Lets see the value for further analysis – Ravindra babu Sep 26 '15 at 14:54
  • Thanks, I was using `output.write(json.toString())` and it worked on Linux, but not in Windows (no error in my java app, but in the server while parsing the request body), I modified my java code and then it worked! on both win and linux. Any ideas why is that? I tested with a couple of machines with both OS, same result always regardless the java version. – Benjamín Guzmán Mar 23 '20 at 05:26