Skip to content

PUT, POST hangs untill timeout if content is empty #119

@sergey-erkin

Description

@sergey-erkin

The problem
My thread hangs when no body passed into PUT or POST params (I use url params only)
But everything is OK if I change method to GET

Possible solution
I suppose we need to do connection.setDoOutput() for PUT and POST methods regardless request content length: see HttpTemplate.java:172

    if (contentLength > 0 || method == POST || method == PUT || method == DELETE) {
        connection.setDoOutput(true);
        connection.setDoInput(true);
    }
    connection.setRequestProperty("Content-Length", Integer.toString(contentLength));

Also we need to open and flush output stream here HttpTemplate.java:114

    if (contentLength > 0  || method == POST || method == PUT || method == DELETE) {
        // Write the request body
        OutputStream out = connection.getOutputStream();
        out.write(requestBody.getBytes(CHARSET_UTF_8));
        out.flush();
    }

Two changes above help me

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions