1

I am sending a HttpsURLConnection request to a server which requires a basic auth with it. I have provided the authorization value and verified it as well. But I'm getting a Http status 500: Internal Server error. Here is my code:

public String httptest(String url, File f, String username, String password) throws Exception {
        // Task attachments endpoint           
        HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
        // Set basic auth header            
        String apiKey = username + ":" + password;
        String basicAuth = "Basic " + new String(new Base64().encode(apiKey.getBytes()));
        connection.setRequestProperty("Authorization", basicAuth);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        logger.info("basicAuth: " + basicAuth);
        // Indicate a POST request
        connection.setDoOutput(true);

        // A unique boundary to use for the multipart/form-data
        String boundary = Long.toHexString(System.currentTimeMillis());
        String fboundary = "----WebKitFormBoundary" + boundary + "u0gW";
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + fboundary);
        // Construct the body of the request            
        logger.info("boundary: " + fboundary);
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));

            String fileName = f.getName();
            String ffname = fileName.substring(fileName.lastIndexOf("\\")+1);
            writer.append("--" + fboundary).append(LINE_FEED);
            writer.append("Content-Disposition: multipart/form-data; name=\"file\"; filename=\"" + ffname + "\"").append(LINE_FEED);
            writer.append("Content-Type: application/x-zip-compressed").append(LINE_FEED); 
            writer.append(LINE_FEED);
            logger.info("fileName: " + fileName);
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
                for (String line; (line = reader.readLine()) != null; ) {
                    writer.append(line).append(LINE_FEED);
                }
            } finally {
                if (reader != null) try {
                    reader.close();
                } catch (IOException logOrIgnore) {
                }
            }

            writer.append(LINE_FEED);
            writer.append("----" + fboundary).append(LINE_FEED);
            writer.append(LINE_FEED);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            System.out.append("Exception writing file" + e);
        } finally {
            if (writer != null) writer.close();
        }
        logger.info("ResponseCode: " + connection.getResponseCode());
        //System.out.println(connection.getResponseCode()); // Should be 200
       return connection.getResponseMessage();
    }

When I send the same request through postman client, It works fine. Here are the headers from the postman request:

Authorization: Basic c2dveWFsQHF1YXJrLmNvbTpRdWFyazEyMw== Cache-Control: no-cache Postman-Token: d6fac5f0-e844-9bac-2bce-51580fdd5557 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="file"; filename="Tiger522.zip" Content-Type: application/x-zip-compressed

----WebKitFormBoundary7MA4YWxkTrZu0gW

Please tell me what to do?

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
CodingIsComplex
  • 291
  • 1
  • 3
  • 12

1 Answers1

0

I can see two possible errors:

  1. As StephaneM pointed out: The content-disposition of the file upload must be "form-data", not "multipart/form-data":

    writer.append("Content-Disposition: form-data; name=\"file\";
    filename=\"" + ffname + "\"").append(LINE_FEED);
    
  2. You specify "Content-Type: application/x-zip-compressed" for the file upload. This means zip format. You are however just writing a file in there, using InputStreamReader. This does not seem to be a zip format that you are writing.

Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
  • Then how do I add the zip file to this request, and it still does not work with Content-Disposition: form-data – CodingIsComplex Dec 15 '15 at 09:37
  • Look a this example how to create a zip file in java: http://stackoverflow.com/questions/1091788/how-to-create-a-zip-file-in-java Basically use ZipOutputStream. An alternative can be to not use zip and specify the mime type accordingly, e.g. "Content-Type: text/plain". – Andreas Vogl Dec 16 '15 at 09:47