1

I am opening an HttpURLConnection and with POST method, I am sending a JSON request that I build form another class. The JSON is structured correctly since I have validated it on debugging. The exception is thrown when trying to read the output response given from the server. This is the Error given

java.io.IOException: Server returned HTTP response code: 400 for URL:

However when I manually try to enter the Url from a web browser with a POST method chrome extension. I can view the response and everything works. So I am sure it has something to do with the following code where I make the connection and read/write.

    URL obj = new URL(url);

    HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
    //add request header
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    //mapping objects to json
    BatchRequest requestParameters = new BatchRequest(o,d);

    ObjectMapper mapper = new ObjectMapper();

    String json = mapper.writeValueAsString(requestParameters);

    connection.setDoOutput(true);
    DataOutputStream os = new DataOutputStream(connection.getOutputStream());

    os.writeBytes(json);
    os.flush();
    os.close();
         // this is where the program throws the exception  
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    String inputLine;
    StringBuilder response = new StringBuilder();

    while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
    }
    in.close();

Both the URL and the JSON request are correct since They work when I try a manual conenction over a browser.

bcsta
  • 1,963
  • 3
  • 22
  • 61
  • I think [this answer](https://stackoverflow.com/a/19671511/4000491) might be helpful for your query. – Bushra Hannure Feb 22 '18 at 13:53
  • Perhaps you need to send Content-Length header too. Try to do your post with postman or soapui, and try to mimic their behaviour as closely as possible. – Gerard H. Pille Feb 22 '18 at 13:53
  • @GerardH.Pille but on my chrome client extention I do not set content length in header, only content type and it works. – bcsta Feb 22 '18 at 13:56
  • @BushraHannure The json is correctly formed since I am using the same json manually when making a POST request on a chrome window using a client extention that allows POST method requests – bcsta Feb 22 '18 at 13:59
  • And you are sure your client extension does not add that header by itself? – Gerard H. Pille Feb 22 '18 at 13:59
  • I found another example where "OutputStream os = con.getOutputStream();". https://www.journaldev.com/7148/java-httpurlconnection-example-java-http-request-get-post – Gerard H. Pille Feb 22 '18 at 14:05
  • @GerardH.Pille my client extention is this https://chrome.google.com/webstore/detail/simple-rest-client/fhjcajmcbmldlhcimfajhfbgofnpcjmb. I am not sure if it adds it automatically but why would it if there is a header tab where I can add my headers? The example you gave does not have a content length header. – bcsta Feb 22 '18 at 14:09
  • @GerardH.Pille I replicated the result on postman. it works without putting Content-Length and the body is in raw format. however when body is formatted with 'from data' button it output 400: bad request – bcsta Feb 22 '18 at 14:23
  • The example does not use a DataOutputStream. Do you send "form data"? Perhaps you need "Content-Type: application/x-www-form-urlencoded " ? – Gerard H. Pille Feb 22 '18 at 14:33
  • @GerardH.Pille with Postman it works when i send with raw button and it works. when I send with 'from-data' bad request 400 occurs – bcsta Feb 22 '18 at 14:51
  • @GerardH.Pille I managed to make it work with your comment. DataOutputStream causes 400 bad request while OutputStream works well. If you write it as an aswer I can Confirm it. Thanks – bcsta Feb 22 '18 at 15:04
  • My pleasure, I've finally answered. ;-) – Gerard H. Pille Feb 22 '18 at 15:54

1 Answers1

1

A DataOutputStream is not needed. Just:

OutputStream os = con.getOutputStream();
Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17