Java httpPost not posting data correctly (Apache library)? - java

Quick question as I've tried many debugging options and none have helped identify the problem. I'm using Java and the Apache HttpPost library.
I have httpGet and httpHead working fine currently, however I can't get httpPost to send the variables correctly. I set up a debug PHP script on my server to simply dump the contents of $_POST. Works correct if I do a curl request in terminal.
Code snippet:
// create new HttpPost object
postRequest = new HttpPost(url);
// add post variables
postRequest.setEntity(new StringEntity(body, ContentType.TEXT_HTML));
// execute request
response = executeRequest(postRequest);
The String variable "body" is just a string, currently containing "test=testing". The output from this PHP debug script:
<?php
echo "Input Stream: ";
var_dump(file_get_contents('php://input'));
echo "POST Variables: ";
var_dump($_POST);
?>
is :
Input Stream: string(12) "test=testing"
POST Variables: array(0) {
}
Does anyone know why it's not getting picked up and dumped in the $_POST variable? Highly irritating as I've spent hours on this!
Any help would be appreciated :) thanks

See Access all entries of HttpParams
To build the URI including the parameters, use:
NameValuePair nvp = new BasicNameValuePair("test", "testing");
String query = URLEncodedUtils.format(nvp, "utf-8"); // use your favourite charset here
URI uri = new URI("http://localhost/app?" + query;
HttpPost postReq = new HttpPost(uri);
httpClient.execute(postReq);
This will result in: http://localhost/app?test=testing being requested.

Related

How to make Java HttpPost NOT include url in body

We have some old java code that POSTs some fields and values to a dotnet5 web api - The api is having problems dealing with the body of the POST as it includes the url/uri as the first part of the body.
The Java sends: http://127.0.0.1:5555?producerRef=GREEN&systemId=78&status=false
But the api is expecting something like: producerRef=GREEN&systemId=78&status=false
as per https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#example. If we send a test message via Postman then the api has no problems.
This is the Java code:
List<NameValuePair> params = new ArrayList<NameValuePair>(queryParams.size());
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
// the address is just that, there's NO parameters
HttpPost post = new HttpPost(this.cmAddress.toURI());
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
CloseableHttpResponse response = httpClient.execute(post);
It's quite simple, but always adds the url to the start of the body of the request. If this is the only way to produce this, what could I do to produce something that looks like this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#example
Many Thanks.
This request seems like a GET request rather than a POST since the request params are in the URL. i don't know about the specifications of the Api you're using, but you can try OKHTTP, you can easily copy the code directly from postman
Postman Get example:
Your issue seems to be at below line
HttpPost post = new HttpPost(this.cmAddress.toURI());
This is the only place which will set the POST url ( another way is to use setURI which is not called anywhere in the code sample you have shared).
If you can use a debugger try checking the value of cmAdress variable

POST request to server using java URLConnnection with params and file inputs

POST request to server using java URLConnnection
I need to send a POST request with the two parameters below:
param1=value1
param2=value2
And also I need to send a file.
In the case of Apache these 2 two(sending params and file) things are handled like below
post.setQueryString(queryString) // queryString is url encoded for eg: param1=value1&param2=value2
post.setRequestEntity(entity) // entity is constructed using file input stream with corresponding format
Please let me know if you have anything related to this problem.
Please note: When I try using Google Chrome REST client plug-in, I am getting the response as below (tried with all request content-types)
UNSUPPORTED FILE FORMAT: 'multipart/form-data' is not a supported content-type
Response code is 400.
Try this API from Apache to send request internally with POST method.
The below is the sample Code to use API
List<org.apache.http.NameValuePair> list =new ArrayList<org.apache.http.NameValuePair>();
HttpPost postMethod = new HttpPost("http://yoururl/ProjectName");
list.add(new BasicNameValuePair("param1", "param1 Value")) ;
postMethod.setEntity(new UrlEncodedFormEntity(list));
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(postMethod);
InputStream is = response.getEntity().getContent();

Getting results of WFS request to GeoServer as UTF-8

I have the following code to send a WFS request to a locally running GeoServer instance using the Apache http-client-4.1 library:
ThreadSafeClientConnManager connectionMngr = new ThreadSafeClientConnManager();
DefaultHttpClient httpClient = new DefaultHttpClient(this.connectionMngr);
HttpPost httpPost = new HttpPost(wfsUrl);
httpPost.setEntity(new StringEntity(wsft, HTTP.UTF_8));
log.debug("Submitting WSFT request: " + wsft);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String result = httpClient.execute(httpPost, responseHandler);
log.debug("Result of WSFT request: " + result);
The data I am retrieving from the GIS database is encoded in UTF-8, and all the features I would expect to find are found. However, any special characters are not being printed properly by my debug statements, or being displayed properly in the front end of my application (a Spring MVC web app).
I know the values are being stored correctly in my GIS database as I can see them via SQL client and they are printed as I would expect. I can also see the names of Roads etc which use special characters are being printed properly on my map layers which suggests the GeoServer is configured correctly.
Instead of passing in a BasicResponseHandler, use the HttpClient.execute(HttpUriRequest) method which returns a HttpResponse and then use EntityUtils.toString(HttpEntity, "UTF-8"), pseudo:
HttpResponse r = httpClient.execute(httPost)
String utf8encodedEntity = EntityUtils.toString(r.getEntity(), "UTF-8");

How can i change charset encoding in HTTP response in Java

I have to fetch some JSON object from a remote server and for that i am using this function which is working great except that for sometime some weird data is getting fetched which i believe is because it is using ASCII charset to decode.
Please find below thw method that i am using
public HttpResponse call(String serviceURL,String serviceHost,String namespace,String methodName,String payloadKey, String payloadValue) throws ClientProtocolException,IOException,JSONException
{
HttpResponse response = null;
HttpContext HTTP_CONTEXT = new BasicHttpContext();
HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0");
HttpPost httppost = new HttpPost(serviceURL);
httppost.setHeader("User-Agent",Constants.USER_AGENT_BROWSER_FIREFOX);
httppost.setHeader("Accept", "application/json, text/javascript, */*");
httppost.setHeader("Accept-Language","en-US,en;q=0.8");
httppost.setHeader("Content-Encoding", "foo-1.0");
httppost.setHeader("Content-Type", "application/json; charset=UTF-8");
httppost.setHeader("X-Requested-With","XMLHttpRequest");
httppost.setHeader("Host",serviceHost);
httppost.setHeader("X-Foo-Target", String.format("%s.%s", namespace,methodName));
/*Making Payload*/
JSONObject objectForPayload = new JSONObject();
objectForPayload.put(payloadKey, payloadValue);
StringEntity stringentity = new StringEntity(objectForPayload.toString());
httppost.setEntity(stringentity);
response = client.execute(httppost);
return response;
}
All these headers that i am passing are correct and i have verified the same via inspect element in Google chrome or Firebug plugin if you are familiar with Mozilla.
Now the problem is that most of the time i am getting the readable data but sometimes i do get unreadable data.
I debugged using eclipse and noticed that the charset under wrappedEntity is showing as "US-ASCII". I am attaching a jpg for reference
Can someone please tell me how can i change the charset from ASCII to UTF-8 of the response before i do response = client.execute(httppost); .
PS:As you have noticed that i am passing charset=utf-8 in the header and that i have already verified using firebug and google chrome that i am passing the exact headers .
Please zoom in to see the image more clearly
Thanks in advance
i was able to resolve the issue just mentioning it for people that may face similar issue.
after getting the response first get the entity by using
HttpEntity entity = response.getEntity();
and since my response was a json object convert entity to string but using "UTF-8" something like this
responseJsonObject = new JSONObject(EntityUtils.toString(entity,"UTF-8"));
previously i was just doing
responseJsonObject = new JSONObject(EntityUtils.toString(entity));
I don't think it's a problem with your headers, I think it's a problem with your string. Just having the header say it's utf-8 doesn't mean the string you write is utf-8, and that depends a lot on how the string was encoded and what's in the "payloadValue"
That said, you can always re-encode the thing correctly before sending it across the wire, for example:
objectForPayload.put(payloadKey, payloadValue);
StringEntity stringentity = new StringEntity(
new String(
objectForPayload.toString().getBytes(),
"UTF8"));
See if that works for you.
You may need to add an "Accept-Encoding"-header and set this to "UTF-8"
Just for the record: the "Content-Encoding" header field is incorrect - a correct server would reject the request as it contains an undefined content coding format.
Furthermore, attaching a charset parameter to application/json is meaningless.
bourne already answered that in the above comment though.
Changing entity = IOUtils.toString(response.getEntity().getContent())
TO entity = EntityUtils.toString(response.getEntity(),"UTF-8")
did the trick.

Query parameters sent with HttpPut Request not being read properly

I am trying make an HttpPut Request to the server and send some parameters with it, but however I think that the parameters are not being detected due to which the server send an error message.
My Code is:
URI url = new URI("http://myurl.com/something/something");
HttpClient client = new DefaultHttpClient();
HttpPut hput = new HttpPut(utl);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("id",URLEncoder.encode(ppid,"UTF-8")));
pairs.add(new BasicNameValuePair("name",URLEncoder.encode(netid,"UTF-8")));
hput.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse res = client.execute(hput);
System.out.println(res.getStatusLine);
It says, the PUT method is not supported by the server but the server does support it.
Tried to do a lot research but wasn't successful as most of the posts were just for POST and GET.
Any help would be greatly appreciated.
Thanks.
Most of the servers PUT and DELETE methods are disabled default. Only GET/POST are enabled.

Categories

Resources