0

I am trying to send an HTTP POST request using java. I was able to send a simple json message but when I try to add multiple properties to the json message I get a 400 bad request response. I am attempting to use Gson to_json in order to properly display it. Does anyone see where I am going wrong?

public boolean Record(String message, LevelType levelType)
{
    try
    {

        URL url;
        url = new URL(String.format("https://intake.opbeat.com/api/v1/organizations/%s/apps/%s/errors/", this.OrganizationId, this.AppId));
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestProperty  ("Authorization", "Bearer " + this.SecretToken);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        //write parameters
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        JsonObject test = new JsonObject();
        test.addProperty("Application ", ApplicationName);
        test.addProperty("Environment ", environment.name());
        test.addProperty("error ", message);
       String testAsJson = new Gson().toJson(test);
        writer.write(testAsJson);
        writer.flush();
        writer.close();

        // Get the response
        int responseCode = conn.getResponseCode();
        StringBuffer answer = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null)
        {
            answer.append(line);
        }
        reader.close();
        //Output the response
        CustomLog.printlnVerbose(answer.toString());
    }
    catch (MalformedURLException ex)
    {
        CustomLog.println(ex);
    }
    catch (IOException ex)
    {
        CustomLog.println(ex);
    }
    //Send an http post to opbeat
    return true;
}

}

Joel Scalera
  • 73
  • 1
  • 9
  • Can you also post the version that currently works? – nclark May 20 '16 at 17:53
  • Possible duplicate of [How to send Request payload to REST API in java?](http://stackoverflow.com/questions/15570656/how-to-send-request-payload-to-rest-api-in-java) – Gangaraju May 20 '16 at 17:55
  • Have you checked https://opbeat.com/docs/api/intake/v1/#-building-the-json-packet- ? The JSON fields you are adding don't look quite right. – Mark May 21 '16 at 05:42

2 Answers2

0

Please save this file on your project:

jetty-web.xml

<?xml version="1.0"?>
  <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
  "http://jetty.mortbay.org/configure.dtd">

  <Configure id="WebAppContext" class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="maxFormContentSize" type="int">800000</Set>
  </Configure>
Put location your project: ..../web/WEB-INF/jetty-web.xml

maxFormContentSize = set your size who many you want.

Bipil Raut
  • 244
  • 1
  • 10
0

I figured it out. I needed to do a couple things.

  1. change "writer.write(testAsJson)" to "writer.write(testAsJson.to_string)"

  2. When adding a second json object , instead of "testAsJson.addProperty" I needed to use "testAsJson.add".

That resolved my errors and successfully completed the POST request. I really appreciate the answers posted though!

Joel Scalera
  • 73
  • 1
  • 9