1

I am trying to upload a file using PUT method. I have been advised before to use POST and do a multipart upload but my web service only allows GET, PUT, PATCH, DELETE, HEAD and OPTIONS. And the main reason for that is that I am updating the JSON array that contains id, target, source, file. Now I just have to update the file part in the web service i.e. I have to upload the file there.

Here is my code:

URLConnection urlconnection = null;
    try{
        File file = new File("/Users/pathtofile/file.pdb");
        URL url = new URL("http://example.website/api/jobs/5/");
        urlconnection = url.openConnection();
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);

        if (urlconnection instanceof HttpURLConnection) {
            try {
                ((HttpURLConnection)urlconnection).setRequestMethod("PUT");
                ((HttpURLConnection)urlconnection).setRequestProperty("Content-Type",
                        "application/json");

                ((HttpURLConnection)urlconnection).connect();
            } catch (ProtocolException e) {
                // TODO Auto-generated catch block
                System.out.println("ProtocolException");
                e.printStackTrace();
            }
        }
        BufferedOutputStream bos = new BufferedOutputStream(urlconnection
                .getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                file));
        int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) >= 0) {
            bos.write(i);
        }
        bos.close();
        // Getting error here (Bad request).
        System.out.println(((HttpURLConnection)urlconnection).getResponseMessage());

    } catch(Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    try {
        InputStream inputStream;
        int responseCode = ((HttpURLConnection)urlconnection).getResponseCode();
        if ((responseCode >= 200) && (responseCode <= 202)) {
            inputStream = ((HttpURLConnection)urlconnection).getInputStream();
            int j;
            while ((j = inputStream.read()) >= 0) {
                System.out.println(j);
            }
        } else {
            System.out.println(responseCode);
            inputStream = ((HttpURLConnection)urlconnection).getErrorStream();
            System.out.println(inputStream);
        }
        ((HttpURLConnection)urlconnection).disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("IOException");
        e.printStackTrace();
    }

And I get the following error:

Bad Request
400

Is there a way to send a file using PUT method? The file is going to be in a place lıke this:

{"target":["This field is required."],
"project":["This field is required."],
"option":["This field is required."],
"result_file":["No file was submitted."],
"com_status":["This field is required."]}

The above is output from curl PUT method that I tried without giving any parameters. And it also shows that the parameters are required in a JSON format.

Mohammad Sohaib
  • 577
  • 3
  • 11
  • 28
  • I don't see any obvious problem in your code, could you test with curl to see what's going on? Try something like this: _curl -v -H "Content-Type: application/json" -X PUT -d @/Users/pathtofile/file.pdb http://example.website/api/jobs/5/_ – Nicolas Filotto Apr 19 '16 at 10:28
  • No, there is no problem. With curl, it works perfect. I just want to know how to upload a file. The web service expects a JSON array. while I am sending only ONE file. I should be sending all the parameters in a JSON format INCLUDING the file. I don,t know how to do that using PUT. – Mohammad Sohaib Apr 19 '16 at 10:53
  • do you mean multipart request? If so check this http://stackoverflow.com/a/16241207/1997376 – Nicolas Filotto Apr 19 '16 at 11:08
  • PUT doesn't use multipart request. :/ – Mohammad Sohaib Apr 19 '16 at 11:18
  • PUT is only a method name nothing more, It could be "FOO" or "BAR", it only depends on your sever actually. If I wanted I could push some content on a sever with a GET method. – Nicolas Filotto Apr 19 '16 at 11:27
  • Maybe if you show the code of your sever, I could help you more – Nicolas Filotto Apr 19 '16 at 11:29
  • My server allows GET. Is there a proper tutorial that can fulfill my needs. Making sure that I don't want to create a new JSON object. I just want to overwrite the previous JSON with the same values, only the file will be a new parameter. – Mohammad Sohaib Apr 19 '16 at 11:44

0 Answers0