Randall Lashley
Randall Lashley

Reputation: 1

POST in JAVA using HttpURLConnection getting a HTTP response code 400

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

Upvotes: 0

Views: 2106

Answers (2)

IRvanFauziE
IRvanFauziE

Reputation: 852

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

Upvotes: 1

Randall Lashley
Randall Lashley

Reputation: 1

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.

Upvotes: 0

Related Questions