Error posting files with RequestBuilder, 403 Forbidden cloudflare - java

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

Related

POST request body cannot contain chinese characters in Java

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");

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

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

HTTPClient's PATCH method not allowing body entity?

I am using Apache HTTPClient version 4.3.5.
I am trying to create a HTTP PATCH request.
HttpPatch request = new HttpPatch(ServerURL);
StringEntity params = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
request.setEntity(params);
client.execute(request);
On checking the actual request received, it didn't have a body associated with it.
Similar code is working fine for HTTP POST requests.

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.

Categories