Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

500 Internal error from HTTP POST request

Tags:

java

android

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

like image 988
Arpit Avatar asked May 15 '15 11:05

Arpit


People also ask

What does a 500 HTTP response indicate?

The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

Is a 500 error my fault?

If you try to visit a website and see a “500 Internal Server Error” message, it means something has gone wrong with the website. This isn't a problem with your browser, your computer, or your internet connection. It's a problem with the site you're trying to visit.


1 Answers

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.

like image 109
StenSoft Avatar answered Oct 26 '22 18:10

StenSoft