0

I've been working with the Slack API in java and have been trying to get an HTTP method that can be used like my below example block of code. That code block works, but the issue is I need to also include a 200 response code, and can't figure out how to get it to work.

Basically, how can I, In Java, send an HTTP post and also tag on the 200 status code using the URL and the content?

Current code:

public void httpRequest(URL url, String content) {
 try {
  byte[] contentBytes = content.getBytes("UTF-8");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setDoInput(true);
  connection.setDoOutput(true);
  connection.setRequestProperty("Accept-Charset", "UTF-8");
  connection.setRequestProperty("Content-Type", "application/json");
  connection.setRequestProperty("Content-Length", Integer.toString(contentBytes.length));
  connection.setRequestProperty("Status", Integer.toString(200));

  OutputStream requestStream = connection.getOutputStream();
  requestStream.write(contentBytes, 0, contentBytes.length);
  requestStream.close();

  String response = "";
  BufferedReader responseStream;
  response = "" + ((HttpURLConnection) connection).getResponseCode();
  try {
   if (((HttpURLConnection) connection).getResponseCode() == 200) {
    responseStream = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
   } else {
    responseStream = new BufferedReader(new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8"));
   }
   response = responseStream.readLine();
   responseStream.close();
  } catch (NullPointerException ignored) {

  }
 } catch (IOException e) {
  e.printStackTrace();
 }
}
cameronlund4
  • 537
  • 1
  • 5
  • 27
  • Are you trying to check for a 200 response from the server? It sounded a bit like you're trying to send one somehow. *"tag on the 200 status code using the URL and the content"* is very confusing. – Andrew Regan Mar 21 '16 at 23:26
  • Also do you need to use the `HttpURLConnection` class rather than an HTTP library? It's much harder. – Andrew Regan Mar 21 '16 at 23:29
  • @AndrewRegan No, I'm not checking for it, I'm trying to add one to the message I'm sending. And can you link me to documentation for the HTTP library you're talking about? – cameronlund4 Mar 21 '16 at 23:33

1 Answers1

0

The call to setDoOutput(true) triggers a post, i.e. you do not need to add

connection.setRequestMethod("POST");

Adding a status header to the request is possible, as you have done, but typically one associates status codes with http responses, not requests. - And off course, adding such custom header would only make sense if the server was designed to use this information to anything.

See this big, and highly up-voted answer on java.net.HttpURLConnection.

Also, you have some problems relating to your response variable as well as the BufferedReader. You accidentally override the value you initially assigned to the response field, instead of concatening. Also, your readLine() should probably be in a loop:

String tmp;
while ((tmp = responseStream.readLine()) !=null){
      response += tmp;
}
Community
  • 1
  • 1
Hervian
  • 1,066
  • 1
  • 12
  • 20
  • This is supposed to be a response to a Slack's slash command call (https://api.slack.com/slash-commands) The message appropriately shows up in slack, however I know slack isn't recognizing my method of tagging the code as the error message appears in chat (ctrl + f for "http 200") Is there another way I should be responding? – cameronlund4 Mar 21 '16 at 23:56