14

I am using below code to upload file using HTTP POST, but I am getting 500 Internal Server Error response from server.

Can you please have a look and let me know which code part is culprit/missing. There is no error in HTTPS connection, I think some problem in Header so server is not accepting this request.

    // Check server address
    url = new URL("https://example.com");
    String protocol = url.getProtocol(); 
    String host = url.getHost();
    String serviceRoot = url.getPath();

    // Build POST request
    HttpPost post = new HttpPost(new URI(protocol + "://" + host
            + serviceRoot));
    post.addHeader("User-Agent", "Test");
    post.addHeader("Content-type", "multipart/form-data"); 
    post.addHeader("Accept", "image/jpg"); 
    String authValue = "Basic "
            + Base64
                    .encodeBase64ToString(("username" + ":"
                            + "password").getBytes()) + " " + "realm=\"example.com\"";
    if (authValue != null) {
        post.addHeader("Authorization", authValue);
    }

    File file = new File("/sdcard/Download/IMAG0306.jpg");
    FileBody data = new FileBody(file);

    String file_type = "jpg" ;
    String description = "Test";

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("file_name", new StringBody( file.getName() ) );
    reqEntity.addPart("description", new StringBody(description));
    reqEntity.addPart("file_type", new StringBody(file_type));
    reqEntity.addPart("data", data);

    post.setEntity(reqEntity); 

    if (true) {
        String trace = ">>> Send HTTP request:";
        trace += "\n " + post.getMethod() + " "
                + post.getRequestLine().getUri();
        System.out.println(trace);
    }

    if (true) {
        String trace = "<<< Send HTTP request-->:";
        trace += "\n" + post.toString();
        Header[] headers = post.getAllHeaders();
        for (Header header : headers) {
            trace += "\n" + header.getName() + " " + header.getValue();
        }
        System.out.println(trace);
    }

    HttpClient httpClient = createHttpClient();
    // replace with your url
    // “Authorization”, “Basic ” + encodedUsernamePassword);
    if (httpClient != null) {
        response = httpClient.execute(post);
        if (true) {
            String trace = "<<< Receive HTTP response:";
            trace += "\n" + response.getStatusLine().toString();
            Header[] headers = response.getAllHeaders();
            for (Header header : headers) {
                trace += "\n" + header.getName() + " " + header.getValue();
            }
            System.out.println(trace);
        }
    } else {
        throw new IOException("HTTP client not found");
    }

Thanks

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
Arpit
  • 143
  • 1
  • 1
  • 6
  • 2
    Take a look at your server logfiles. 500 is an internal server error. So you should find the solution there. – Jens May 15 '15 at 11:39
  • 2
    500 is a server side error, you can test your request in a http rest client (postman for example, a browser extension), with the exact same params, it will end up with the same result. – Mostrapotski May 15 '15 at 11:39
  • 3
    My huntch is that your request exceeds MaxRequestLen, which is usually 131072 bytes, you can try with a small file to see if it works, but in order to be completely sure tail your server error logs while making the request. tail -f /var/log/apache2/error.log – Pavel Petrov May 15 '15 at 11:43
  • @PavelPetrov Shouldn't that return 413 Request Entity Too Large? – StenSoft May 15 '15 at 12:13
  • 1
    @StenSoft, nope I'm looking in a server log of an Apache 2.4.7 and in the error.log mod_fcgid: HTTP request length 139072 (so far) exceeds MaxRequestLen (131072), And the http side is the Internal Error 500 screen of death :) For the last 5 years, I've never seen Apache return 413 Request Entity Too Large. – Pavel Petrov May 15 '15 at 12:30
  • @PavelPetrov Hmm, it seems the FastCGI handler closes connection instead of returning 413 so for Apache, it looks like it crashed. I don't know about Apache but if you set the limit in nginx's configuration, it returns 413. – StenSoft May 21 '15 at 16:10

2 Answers2

22

500 Internal Server Error is a server error, ie. the problem is on the server side, not client side. You need to check the server logs to see what the problem is.

The headers are fine. If the headers were wrong, you would get 400 Bad Request or some other 4xx error instead.

StenSoft
  • 9,369
  • 25
  • 30
0

Check the server logs, especially if the endpoint is not being hit. In my case the error was: The request matched multiple endpoints. I had put other functions in the controller, which it thought were endpoints, but were not.

Markus
  • 1,020
  • 14
  • 18