I have a simple method to send a POST request:
public HttpResponse post(InputStream content, String contentType, URI url) {
InputStreamEntity entity = new InputStreamEntity(content);
entity.setChunked(true);
entity.setContentType(contentType);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity)
return httpClient.execute(httpPost, httpContext);
}
The httpPost seems well configured:
httpPost.getEntity().toString() = [Content-Type: application/json,Chunked: true]
httpPost.getEntity().getContentLength() = -1
But the remote server receives Content-Length header
A request on http://httpbin.org/post shows the actual headers are:
"headers":{
"Accept-Encoding": "gzip,deflate",
"Content-Length": "571",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "blabla",
"Via": "1.1 localhost (Apache-HttpClient/4.5.2 (cache))"
}
=> Does org.apache.http.client 4.5 really support chunked-encoding or does it fake it ?
Thank you
Chunked data is definitely supported by Apache HttpClient.
Httpbin.org relies on Nginx and I guess buffering of proxy's requests is enabled in their configuration. Consequently, you do not see chunked transfer encoding in the result returned by httpbin.
Instead of using an external service such as httpbin.org for checking this kind of headers, use your own webserver.
Related
I'd like to test (via automated test) how server (and all proxies in-the-middle) responds to a PUT request without body and Content-Length header.
Similar to what curl does
curl -XPUT http://example.com
with Apache HTTP client (4.5.13)
But it looks like it always adds Content-Length header if I specify no body.
Is there any way to do that with Apache HTTP client?
Already tried (no luck)
final HttpPut request = new HttpPut(url);
request.removeHeaders("Content-Length");
Use a request interceptor to modify requests generated by the standard protocol processor
CloseableHttpClient httpClient = HttpClients.custom()
.addInterceptorLast((HttpRequestInterceptor) (request, context) ->
request.removeHeaders(HttpHeaders.CONTENT_LENGTH))
.build();
HttpPut httpPut = new HttpPut("http://httpbin.org/put");
httpClient.execute(httpPut, response -> {
EntityUtils.consume(response.getEntity());
return null;
});
I am trying to use a web API in a Java program using Apache HttpClient5.
Using a simple request with curl:
curl -X POST -H "x-api-user: d904bd62-da08-416b-a816-ba797c9ee265" -H "x-api-key: xxxxxxxxxxx" https://habitica.com/api/v3/user/class/cast/valorousPresence
I get the expected response and effect.
Using my Java code:
URI uri = new URIBuilder()
.setScheme("https")
.setHost("habitica.com")
.setPath("/api/v3/user/class/cast/valorousPresence")
.build();
Logger logger = LoggerFactory.getLogger(MyClass.class);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader(new BasicHeader("x-api-user",getApiUser()));
httpPost.addHeader(new BasicHeader("x-api-key", getApiKey()));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
logger.info(httpResponse.toString());
return httpResponse.getCode();
The output I get when running the Java call is
411 Length Required HTTP/1.0
I'm sure I'm not constructing the POST call correctly, how should it be done? I've tried specifying Content-Type and that has no effect. Trying to set Content-Length in the code causes compilation errors (as I understand it, this is handled behind the scenes by HttpClient5).
All my GET requests using HttpClient5 work fine.
A POST always has a payload (content). A POST without content is unusual, so are you sure you didn't forget something?
You need to call setEntity() to set the payload, even if it is empty, because it is the entity that sets the Content-Length header.
E.g. you could call httpPost.setEntity(new StringEntity("")), which sets Content-Type: text/plain and Content-Length: 0.
I have a strange situation going on while implementing Web service client.
Request fired from SOAP UI is success and the HTTP request is as below:
POST http://xxxxxxxxxx/xx/xx/xxxxx/xxxxx HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "CreateUserSoap"
Content-Length: 1490
Host: xxxxxxxxxxxxxxxxxxx.net
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Cookie: SMCHALLENGE=YES
Cookie2: $Version=1
Authorization: Basic ZHFhbgfdd6RFFQcdfgdm9vccQ=
<soap........./>
However when i fire the web service request from the application, for which i use apace cxf following HHTP Request is generated and i get a 403:Forbidden Error
ID: 1
Address: http://xxxxxxxxxx/xx/xx/xxxxx/xxxxx
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], Authorization=[Basic ZHFhbgfdd6RFFQcdfgdm9vccQ=], SOAPAction= ["CreateUserSoap"]}
Payload: <soap....... />
I am not sure if the apache - cxf is generating the right http header as expected by the server. the soap envelope is same in both cases.
Below is the implementing java client code:
ClientWs clientWS= new ClientWs ();
ClientWSPortType portType = clientWS.getClientWSPort();
BindingProvider provider = (BindingProvider) portType;
Header dummyHeader = new Header(new QName("http://clientWS/wsdl", "ClientWS"), documentBuilder.parse(new ByteArrayInputStream(xmlString2.getBytes("UTF-8"))).getDocumentElement() ); --parsing the soap mesage
headers.add(dummyHeader);
provider.getRequestContext().put(Header.HEADER_LIST, headers);
provider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, user_name);
provider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, pass_word);
Well, no one ansered neither was i able to do this with Apache CXF.
The turnaround was to use core java HttpURLConnection to achieve this.
A method something like this helped the cause ad post a webservice call without any framework.:
public HttpURLConnection getHttpConn(String webservice_url) throws IOException {
URL endpoint = new URL(webservice_url);
URLConnection connection = endpoint.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
byte[] encodedBytes = Base64.encodeBase64((getUsername()+":"+getPassword()).getBytes());
httpConn.setRequestMethod(getRequestMethod());
httpConn.setRequestProperty(HTTP_ACCEPT_ENCODING, getAccept_Encoding());
httpConn.setRequestProperty(HTTP_CONTENT_TYPE, getContentType());
httpConn.setRequestProperty(getContent_Length(), getContent_Length());
httpConn.setRequestProperty(HTTP_HOST, getHost());
httpConn.setRequestProperty(getConnection(), getConnection());
httpConn.setRequestProperty(HTTP_COOKIE2, getCookie2());
httpConn.setRequestProperty(HTTP_COOKIE, getCookie());
httpConn.setRequestProperty("Authorization", "Basic "+new String(encodedBytes));
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
return httpConn;
}
For example the default user agent could be set like:
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, someName);
But how to set the "Accept" header?
HttpClient 4.3 now allows configuring a collection of default headers on the client itself:
Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
List<Header> headers = Lists.newArrayList(header);
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
client.execute(request);
Now, all requests executed by that client will be send with the default headers.
Hope that helps.
I have a server which expects Content-Length as a part of POST header. For POSTing, I am using Apache HttpComponents library.
This is a stripped down version of the expected request (with all the required headers ofcourse):
POST /ParlayREST/1.0/sample/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: server.url.com:8080
Content-Length: 287
{
"listenerURL":"http://application.example.com/notifyURL"},
"sessionId":"12345"
}
I have used the setEntity method of HttpPost to set a StringEntity (converted json -> String -> StringEntity) as content of the POST. But when I execute the request, I end up with a POST request which doesn't specify Content-length within it's header.
Is there anyway to add this missing header?
(I tried setHeader() to set the Content-Length which threw an error saying that the content length is already present)
This is the code that I am using to create the POST request:
//Convert the registration request object to json
StringEntity registrationRequest_json_entity = new StringEntity(gsonHandle.toJson(registrationRequest));
registrationRequest_json_entity.setContentType("application/json");
//Creating the HttpPost object which contains the endpoint URI
HttpPost httpPost = new HttpPost(Constants.CLIENT_REGISTRATION_URL);
httpPost.setHeader(HTTP.CONTENT_TYPE,"application/json");
httpPost.setHeader("Accept","application/json");
httpPost.setHeader(HTTP.TARGET_HOST,Constants.REGISTRATION_HOST + ":" + Constants.REGISTRATION_PORT);
//Set the content as enitity within the HttpPost object
httpPost.setEntity(registrationRequest_json_entity);
HttpResponse response = httpClient.execute(httpPost, new BasicHttpContext());
HttpEntity entity = response.getEntity();
if (entity != null) {
//work on the response
}
EntityUtils.consume(entity);
HttpClient automatically generates Content-Length and Transfer-Encoding header values based on properties of the enclosed message entity and the actual protocol settings.
Do not set those headers manually.
Try:
httppost.setHeader(HTTP.CONTENT_LEN,"0");
This will set the content length to 0.