I am using Hibernate and GSON to retrieve data as an object in Java, and create a .toString method via GSON in the POJO to make JSON string. Now I need to use this JSON as a request body to send a POST request to a web service.
The problem is that if the JSON contains any chinese character, it will not work. It complains the chinese name field is incorrect. I checked and there are no spaces in the chinese string, and the .length() of that is exactly the number of characters.
I also tried to copy the whole JSON and paste it to Postman to make the POST request. It will work. How come it does not work in my Java code but works in Postman? Below is how I create the POST request.
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(CONNECTION_TIMEOUT)
.setConnectTimeout(CONNECTION_TIMEOUT)
.setSocketTimeout(CONNECTION_TIMEOUT)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
StringBuilder entity = new StringBuilder();
entity.append(myJsonFromPojo);
HttpPost post = new HttpPost(uri);
post.addHeader("content-type", "application/json");
post.addHeader("Content-Type", "charset=UTF-8");
post.addHeader("Accept", "*/*");
post.addHeader("Accept-Encoding", "gzip,deflate");
post.addHeader("Authorization", "Bearer " + token);
post.setEntity(new StringEntity(entity.toString()));
response = httpClient.execute(post);
Manage to use below to solve my issue. post.addHeader("Content-type", "application/json; charset=UTF-8"); post.addHeader("Accept", "application/json");
Related
I'm trying to make a request to send an mp3 file via HttpUriRequest multipartRequest, I've added all the headers that the API documentation asks for (Content-Type multipart/form-data).
However, a strange thing I noticed was the "Host" header, when I make a request in Postman, it goes successfully, but when I request my implementation, I get the following error:
400 Bad Request - cloudflare
My code:
CloseableHttpClient httpclient = HttpClients.createDefault();
MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entitybuilder.addBinaryBody("file", new File("/C:/64551f6c78bc742885a2f0b8100cb039-
recording.mp3"));
HttpEntity mutiPartHttpEntity = entitybuilder.build();
RequestBuilder reqbuilder =
RequestBuilder.post("https://api.pipedrive.com/v1/callLogs/"+idActive+"/recordings?
api_token=a09e26295e89cb2ccc89b676358deae384046449");
reqbuilder.setEntity(mutiPartHttpEntity);
reqbuilder.addHeader("Content-Type", "multipart/form-data");
reqbuilder.addHeader("Accept", "*/*");
reqbuilder.addHeader("Accept-Encoding", "gzip, deflate, br");
reqbuilder.addHeader("Connection", "keep-alive");
reqbuilder.addHeader("Host", "http://localhost:8080/attach");
//Building the request
HttpUriRequest multipartRequest = reqbuilder.build();
multipartRequest.addHeader("Content-Type", "multipart/form-data");
HttpResponse httpresponse = httpclient.execute(multipartRequest);
System.out.println(EntityUtils.toString(httpresponse.getEntity()));
System.out.println(httpresponse.getStatusLine());
If anyone has been through this, any help will be of great value.
You are adding the "multipart/form-data" after you already build your Request, move it up a line
I put this line up but I still get an error, there was a mistake, the part before building the RequestBuilder reqbuilder I already added all the headers
I was trying to post some data to AWS EC2 rest end-point, from an AWS Lambda. I was getting response "with status code: 406 and with status: Not Acceptable". I tried fixing this, with adding accept headers as below
HttpPost httpPost = new HttpPost(serviceURL);
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/xml");
httpPost.addHeader(HttpHeaders.ACCEPT, "*/*");
httpPost.addHeader(HttpHeaders.ACCEPT_CHARSET, "*");
httpPost.addHeader(HttpHeaders.ACCEPT_ENCODING, "*");
httpPost.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "*");
httpPost.setEntity( new InputStreamEntity(content, ContentType.APPLICATION_XML) );
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httpPost);
Am i missing something? Is there anything else that i should be looking into?
Attach exception log please. Are u trying to send this method though any kind of front
The problem:I have a few forms in the html page which I want to edit, then submit the data.
I have read about entities in HttpClient, and I came across the UrlEncodedFormEntity, which as far as I understand you add parameters to it and then you can post them. I find this ok, but I thought is there a different way to post the changed attributes, since jsoup has a convenient method to set a value in an attribute. this is what I tried using a different entity, StringEntity:
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Charset", "UTF-8");
post.setHeader("Cookie", getCookies());
post.setHeader("Connection", "keep-alive");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new StringEntity(updatedHTML, ContentType.TEXT_HTML));
HttpResponse response = null;
response = client.execute(post);
where updatedHTML is the full html code with the changes I want to post.
but as you guessed, its not working.
edit: I don't think it's the problem, but I also have a sumbit button, which I ignored here, should it also be considered in the updatedHTML?
Thanks for help.
Two things are wrong in your approach.
you cannot pass an html in the StringEntity as it is not the usage of the class
The StringEntity, as well as its derived classes, is intended to carry messages.
The second error is that you seem to use the library to change the html.
You need to work on what you are posting. Here an example.
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("your parameter name","your parameter value"));
formparams.add(new BasicNameValuePair("another parameter name","another paramete value"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("http://localhost/");
httppost.setEntity(entity);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
I made some assumptions:
you have in your hands all the parameters you are passing (simply you will change the approach not working on the html but on the url)
The exception handling is not considered in my snippet. The code is a simple example to show you how to deal with forms
Note also that the UrlEncodedFormEntity will handle the parameters for you. E.g. in our example>
your parameter name=your parameter value&another parameter name=another parameter value
I am making a SOAP request to a web method using HTTP Post. The request and response are both JSON.
But while making the POST request i am getting an error :
The server cannot service the request because the media type is
unsupported.
This is my code
String SOAP_ACTION = "Method name";
String URL = "service url";
HttpPost httpPost = null;
httpPost = new HttpPost(URL);
httpPost.addHeader("Accept-Encoding", "gzip,deflate");
httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
httpPost.addHeader("SOAPAction", SOAP_ACTION);
HttpEntity postEntity = new StringEntity(requestContent);
httpPost.setEntity(postEntity);
I have tried giving Accept-Encoding as application/json , text. But i still get the same error.
SOAP is an XML-based protocol. See https://en.wikipedia.org/wiki/SOAP and the W3C SOAP Primer.
If you want to communicate using JSON, that's fine - but it isn't SOAP.
I'm trying to post some JSON data in java for an Android app I'm working on. Is the below valid or do I need to push the JSON string in a different way?
HttpPost httpost = new HttpPost("http://test.localhost");
httpost.setEntity(new StringEntity("{\"filters\":true}"));
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
//... other java code to execute the apache httpclient
Thank you in advance
You should set the Content-Type header to "application/json". Everything else looks good.