0

I'm pretty new to making HTTP connections and working with API's in Java, so I'm not sure where the problem lies. When I send out a POST connection request in order to send a JSON formatted String of text to the other side, I get an error back along with a 400 response code. When I look up that code, it seems my connection isn't properly formatted. Code is below, along with the error message. Please help! Thanks!

public void sendToAPI(String urlPass, String param) throws IOException {

    URL url = new URL(urlPass);
    HttpURLConnection connectionOut = (HttpURLConnection) url.openConnection();
    connectionOut.setRequestMethod("POST");
    connectionOut.setConnectTimeout(5000);
    connectionOut.setReadTimeout(5000);
    connectionOut.setRequestProperty("Content-Type", "application/json");
    connectionOut.setRequestProperty("Content-Length", Integer.toString(param.length()));
    connectionOut.setDoOutput(true);
    connectionOut.setDoInput(true);
    connectionOut.connect();

    DataOutputStream stream = new DataOutputStream(connectionOut.getOutputStream());
    stream.writeUTF(param);
    stream.flush();
    stream.close();

    int responsecode = connectionOut.getResponseCode();
    if(responsecode != 200) {
        System.out.println("Response Code is " + responsecode);             
    }

    BufferedReader in = new BufferedReader(
            new InputStreamReader(connectionOut.getInputStream()));
    String output;
    StringBuffer response = new StringBuffer();

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

    //printing result from response
    System.out.println(response.toString());

}

Response Code is 400 Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL:XXX

3
  • what is your url and param? Commented Jan 11, 2019 at 21:01
  • url="vautointerview.azurewebsites.net/api" + datasetId.get("datasetId") + "/answer" param="answer=" + dealers.toString() It's not showing but the url is properly formatted with http:// in front, just a simple String. Same with the parameter, it's just a String, which I don't think would trigger a 400 response code. Commented Jan 11, 2019 at 21:16
  • Does param include multibyte characters? content-length might be wrong. Commented Jan 11, 2019 at 21:57

2 Answers 2

1

u can try this code:

InputStream inputStream;
if (responseCode == 200) {
    inputStream = con.getInputStream();
} else {
    inputStream = con.getErrorStream();
}

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String lines;
while ((lines = reader.readLine()) != null) {
    builder.append(lines);
    builder.append(System.getProperty("line.separator"));
}

String retStr = builder.toString().trim();
reader.close();

System.out.println("retStr: " + retStr);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate, found the root problem in my code by use your snippet.
0

So after playing around with the DataOutputStream, I replaced the below code:

DataOutputStream stream = new DataOutputStream(connectionOut.getOutputStream());
stream.writeUTF(param);

With another example I found online:

OutputStream os = connectionOut.getOutputStream();
os.write(param.getBytes());
os.flush();
os.close();

I'm not sure yet why, but this suddenly got the proper response code I was looking for, so the format it was sent in matched what they requested. Thanks for all responses.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.