0

I'm trying to upload an image with JAVA to a self-hosted ActiveCollab.

I have made a couple of tests and this one seems for me like the most solid of them by far. Anyway, when I try to run it, I get code 200-OK and an empty array as a response. .

public static void main(String args[]) throws IOException  {



        URL url = new URL("<SITE>/api/v1/upload-files");
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setDoOutput(true);
        c.setRequestProperty("Content-Type", "multipart/form-data");
        c.setRequestProperty("X-Angie-AuthApiToken", "<TOKEN>");

        JSONArray array = new JSONArray();
        array.put("/test.png");
        array.put("image/png");

        OutputStream out = c.getOutputStream();
        out.write(array.toString().getBytes());
        out.flush();
        out.close();

        BufferedReader buf = new BufferedReader(new InputStreamReader(c.getInputStream()));
        StringBuffer response = new StringBuffer();
        String line;
        while (null != (line = buf.readLine())) {
            response.append(line);
        }

        JSONArray message = new JSONArray(response.toString());
        System.out.println(message);

}

In the API documentation I should get a filled json array as a response. Actually I don't know at what I'm missing.

Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29
Marko
  • 1
  • 3
  • Do you have logging of the server or a link to the api documentation of the URL you're connecting to ? – Stephan Hogenboom May 08 '19 at 11:25
  • Of course @StephanHogenboom, link. I readed it a couple of times and I don't see nothing about this problem. – Marko May 08 '19 at 12:03
  • Perhaps it is the multipart/form-data part? Maybe this can help you: https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – Stephan Hogenboom May 08 '19 at 12:04
  • @StephanHogenboom I've tooked a look to it, seems interesting and it give me some ideas but I still not being able to solve the problem, the response continue being an empty json... – Marko May 08 '19 at 13:57
  • To be honest if the server returns an empty array i can 't help you, as i can't recreate the problem from here. Can you see the logs the response server side? Or can't access the server logs? – Stephan Hogenboom May 08 '19 at 13:59

1 Answers1

0

Finally I solved it! As @StephanHogenboom said, the problem were in multipart/form-data, the parameters had to be introduced there and not via JSONArray. I didn't find so much information about how to work with multipart in java.net but at least I've found a deprecaded but functional way to the the job.

public static void main(String args[]) throws IOException  {



        URL url = new URL("<SITE>/api/v1/upload-files");
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setDoOutput(true);
        c.setRequestMethod("POST");
        c.setRequestProperty("X-Angie-AuthApiToken", "<TOKEN>");

        File file = new File("/1.png");
        FileBody fileBody = new FileBody(file, "image/png");
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
        multipartEntity.addPart("file", fileBody);

        c.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());

        OutputStream out = c.getOutputStream();
        multipartEntity.writeTo(out);
        out.close();

        BufferedReader buf = new BufferedReader(new InputStreamReader(c.getInputStream()));
        StringBuffer response = new StringBuffer();
        String line;
        while (null != (line = buf.readLine())) {
            response.append(line);
        }

        JSONArray message = new JSONArray(response.toString());
        System.out.println(message);

    }

Actually it works for me, but if someone can give me ideas about how improve it would be great!

Marko
  • 1
  • 3