Jersey Http Client replying with 411 on POST request

蓝咒 提交于 2020-01-05 04:07:32

问题


I'm using Jersey HTTP Client for sending POST request and receiving 411 Content Length missing in response. However this error is not thrown in all the cases. There are some calls where I'm using json content type and it is working fine.

For this particular request where I'm sending the request to a specific URL I'm receiving 411 Content Length required. While with the same payload when I'm using a different URL it is working fine.

This is how I'm setting Form Entity in Jersey Client Request

public static MultivaluedMap<String, String> getMultivaluedMapRequest(Map<String, String> map) {
    MultivaluedMap<String, String> bodyParams = new MultivaluedHashMap<String, String>();

    if (null != map && map.size() > 0) {
        for (Entry<String, String> param : map.entrySet()) {
            bodyParams.add(param.getKey(), param.getValue());
        }
    }
    return bodyParams;
}

private static Entity<?> getRequestEntity(final HttpRequestPayload<?> payload) {
    if (payload.getEntity() instanceof MultivaluedMap<?, ?>) {
        return Entity.form((MultivaluedMap<String, String>) payload.getEntity());
    } else {
        return Entity.entity(payload.getEntity(), payload.getMediaTypeProduced());
    }
}

This is how the call is made

final Entity<?> entity = getRequestEntity(payload);

Response response = target.request(payload.getMediaTypeConsumed()).headers(payload.getHeaders()).post(entity);

I'm not sure what the issue is. I think it has something to do with the URL I'm sending the request to.


回答1:


HttpClientConnectionManager has the ClientProperties.REQUEST_ENTITY_PROCESSING set to CHUNKED by default. As a solution I set it to BUFFERED and it worked.

ClientConfig configuration = new ClientConfig(); configuration.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);



来源:https://stackoverflow.com/questions/37142850/jersey-http-client-replying-with-411-on-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!