I have looked at many questions regarding this same problem and have tried many proposed solutions without success. The one located here seems the most relevant, as the comment by RedSonic answered why this isn't working.
I am trying to make a POST or PUT request to my server using Java's Apache library.
public static void put(String caseNumber,DefaultHttpClient httpClient)
throws Exception {
HttpPut putRequest = new HttpPut(
"http://localhost:8888/servlet/example1/" + caseNumber);
putRequest.addHeader("Version", version);
// putRequest.setHeader("Content-Type", content);
// putRequest.addHeader(new BasicHeader("ContentType", content));
putRequest.setHeader("Accept", accept);
putRequest.setHeader("Date", date);
List <NameValuePair> nvpsPut = new ArrayList <NameValuePair>();
nvpsPut.add(new BasicNameValuePair("group", jsonPostGroup));
putRequest.setEntity(new UrlEncodedFormEntity(nvpsPut));
HttpResponse response = httpClient.execute(putRequest);
}
I have tried creating a separate entity and setting the entity's content type, then set the entity to the request. I've tried other things as well, but I can't manage to get the request to have the content type that I want to be passed in without the parameters I'm attempting to send over going blank.
Have also tried
HttpPut putRequest = new HttpPut("http://localhost:8888/servlet/example1/" + caseNumber);
putRequest.addHeader("Version", version);
putRequest.setHeader("Content-Type", content);
putRequest.setHeader("Accept", accept);
putRequest.setHeader("Date", date);
BasicHttpParams bhp = new BasicHttpParams();
bhp.setParameter("test", jsonPostGroup);
putRequest.setParams(bhp);
HttpResponse response = httpClient.execute(putRequest);
Log:
BasicClientConnectionManager - Get connection for route {}->http://localhost:8888
DefaultClientConnectionOperator - Connecting to localhost:8888
RequestAddCookies - CookieSpec selected: best-match
RequestAuthCache - Auth cache not set in the context
RequestTargetAuthentication - Target auth state: UNCHALLENGED
RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
DefaultHttpClient - Attempt 1 to execute request
DefaultClientConnection - Sending request: PUT /servlet/example1/000001994 HTTP/1.1
wire - >> "PUT /servlet/example1/000001994 HTTP/1.1[\r][\n]"
wire - >> "Version: 1.1[\r][\n]"
wire - >> "Content-Type: application/JSON[\r][\n]"
wire - >> "Accept: vnd.siren+JSON[\r][\n]"
wire - >> "Date: Wed Aug 21 15:30:00 EDT 2013[\r][\n]"
wire - >> "Content-Length: 0[\r][\n]"
wire - >> "Host: localhost:8888[\r][\n]"
wire - >> "Connection: Keep-Alive[\r][\n]"
wire - >> "User-Agent: Apache-HttpClient/4.2.5 (java 1.5)[\r][\n]"
wire - >> "[\r][\n]"
headers - >> PUT /servlet/example1/000001994 HTTP/1.1
headers - >> Version: 1.1
headers - >> Content-Type: application/JSON
headers - >> Accept: vnd.siren+JSON
headers - >> Date: Wed Aug 21 15:30:00 EDT 2013
headers - >> Content-Length: 0
headers - >> Host: localhost:8888
headers - >> Connection: Keep-Alive
headers - >> User-Agent: Apache-HttpClient/4.2.5 (java 1.5)
wire - << "HTTP/1.1 200 OK[\r][\n]"
wire - << "Set-Cookie: JSESSIONID=davwq58qgxr8yvyw7bfybbbv;Path=/[\r][\n]"
wire - << "Expires: Thu, 01 Jan 1970 00:00:00 GMT[\r][\n]"
wire - << "Content-Type: text/html; charset=ISO-8859-1[\r][\n]"
wire - << "Content-Length: 0[\r][\n]"
wire - << "Server: Jetty(9.0.0.v20130308)[\r][\n]"
wire - << "[\r][\n]"
DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
headers - << HTTP/1.1 200 OK
headers - << Set-Cookie: JSESSIONID=davwq58qgxr8yvyw7bfybbbv;Path=/
headers - << Expires: Thu, 01 Jan 1970 00:00:00 GMT
headers - << Content-Type: text/html; charset=ISO-8859-1
headers - << Content-Length: 0
headers - << Server: Jetty(9.0.0.v20130308)
ResponseProcessCookies - Cookie accepted: "[version: 0][name: JSESSIONID][value: davwq58qgxr8yvyw7bfybbbv][domain: localhost][path: /][expiry: null]".
DefaultHttpClient - Connection can be kept alive indefinitely
BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@7842f3a9
BasicClientConnectionManager - Connection can be kept alive indefinitely
Edit: I have found a few other suggestions that I've tried. Such as setting the parameters on the httpclient instead of the request (which doesn't make much sense in this scenario) but I experienced the same issue.
Edit 8/23: I'm still able to pass parameters via the setting the Entity, and giving the entity a name + value pair. However, as mentioned, this hoses the content-type. Any more thoughts before I give up entirely?