infinitesunrise / carsinbikelanes

A browsable geographic database for crowdsourced traffic violation reporting
GNU General Public License v3.0
64 stars 18 forks source link

connecting to upload api endpoint #64

Closed alexfrieden closed 7 years ago

alexfrieden commented 7 years ago

Hi! I was trying to connect through the rest upload endpoint. I am getting "bad request" from the response variable. I looked at the json doc and I think everything is parameterized correctly. Let me know your thoughts! Thanks!

   File image = new File("/Users/alexanderfrieden/Downloads/342.JPG");
    String licensePlateNum = "3GE284";
    String licensePlateState = "MA";
    double latitude = 42.384071;
    double longitude = -71.113609;
    String comment = "this is a comment";
    Map<String, Object> params = new LinkedHashMap<>();
    params.put("image", image);
    params.put("plate", licensePlateNum);
    params.put("state", licensePlateState);
    params.put("date", new Date());
    params.put("gps_latitude", latitude);
    params.put("gps_longitude", longitude);
    params.put("description", comment);
    StringBuilder postData = new StringBuilder();
    for (Map.Entry<String, Object> param : params.entrySet()) {
        if (postData.length() != 0) postData.append('&');
        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
        postData.append('=');
        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
    }
    String request = "http://carsinbikelanesboston.com/api/upload";
    URL url = new URL(request);
    byte[] postDataBytes = postData.toString().getBytes("UTF-8");

    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
    conn.setDoOutput(true);
    conn.getOutputStream().write(postDataBytes);
    String response = conn.getResponseMessage();
    Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

    for (int c; (c = in.read()) >= 0;)
        System.out.print((char)c);