Http POST failing with status code: 406 and with status: Not Acceptable - java

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

Related

Error posting files with RequestBuilder, 403 Forbidden cloudflare

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

Handling HTTP request redirect in java

I'm writing a network android application that uses http requests to get data. The data is HTML format. I use Apache HttpClient and JSoup.
When I'm out of traffic with my mobile internet provider, I am always redirected to the providers' page saying that I should pay some money. Of course, it is a bad idea to parse this page.
How to detect occured page substitution?
This code will help you to know with is the final target of your request, if isn't the page that you asked for, is the provider page.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://www.google.com/");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpHost target = (HttpHost) localContext.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);// this is the final page of the request
System.out.println("Final target: " + target);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
Thanks
If your provider is lying to you by immediately returning a 200 OK but not giving you the resource you've requested, your best option is probably to set a custom HTTP response header that your client can check before continuing.

Making a HTTP POST SOAP request with JSON

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.

Android error : MultipartEntity , request sent by the client was syntactically incorrect

I am suppose to send song (mp3/wav) file and some data through secure restful web service. I am using MultipartEntity to make HttpPost request. but When I execute it through HttpClient, the server replies this error
HTTP Status 400 - Bad Request
type: Status report
message : Bad Request
The request sent by the client was syntactically incorrect (Bad Request).
But the service is doing very well if we call it from its Web interface. please help
its the code
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost();
try {
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("email", new StringBody("test#testmail.com"));
reqEntity.addPart("password", new StringBody("123"));
reqEntity.addPart("title", new StringBody("My new song"));
reqEntity.addPart("musicData", new FileBody(new File(FilePath)));
// FIlePath is path to file and contains correct file location
postRequest.setEntity(reqEntity);
postRequest.setURI(new URI(ServiceURL));
HttpResponse response = httpclient.execute(postRequest);
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
I also included apache-mime4j, httpclient, httpcore and httpmime jars for MultipartEntity.
This is HTML page snap for the Service.
Try removing the setURI method and passing the URL in when you create your HttpPost object, as follows. This worked for me (more here).
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(ServiceURL);
try {
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("email", new StringBody("test#testmail.com"));
reqEntity.addPart("password", new StringBody("123"));
reqEntity.addPart("title", new StringBody("My new song"));
reqEntity.addPart("musicData", new FileBody(new File(FilePath)));
postRequest.setEntity(reqEntity);
HttpResponse response = httpclient.execute(postRequest);
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
It seems header of the request is incorrect, this problem can occur if you use a different Auth protocol or upper/lower case or simply wrong things in header that server side can't handle.
Dont waste your time by trying different different combinations.There are some HTTP Request tools available for HTTP with which you can track request and response you are getting.Ex. HTTP Analyzer download trial version
Call URL from your working webinterface , copy request and response
then do same with from program the tool is enogh capable to capture your request and response data.
Now compare working and non working request you will surely able to dignose the issue whether it can be header issue or some authentication related issue.

Setting locale with DefaultHttpClient?

I'm using DefaultHttpClient to make an http connection. I [think] we can set the preferred locale in the http headers [http accept-language] when making a connection, which the server can check and send back content in a matching language (if it wants).
Anyone know if this is possible, or how to go about doing this with DefaultHttpClient?
Thanks
You have to add your header to HttpRequest object
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(URL);
request.addHeader("Accept-Language", "en");
HttpResponse response = client.execute(request);

Categories