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.
Related
I am attempting to modify items in a Sharepoint list using the Sharepoint REST API from back-end java code. I am using NTLM authentication to access the server and Apache httpclient to perform the http requests. I am able to perform a GET request to the list URL without issue, but when doing a POST request, I am receiving an error:
The type SP.ListItemEntityCollection does not support HTTP PATCH
method
Here is the relevant code for the POST request:
JsonObject type = new JsonObject();
type.addProperty("type", "SP.Data.MyListListItem");
JsonObject listJson2 = new JsonObject();
listJson2.add("__metadata", type);
listJson2.addProperty("Title", "test");
String backToString = listJson2.toString();
CloseableHttpClient httpClient2 = HttpClients.createDefault();
HttpPost httpPost2 = new HttpPost(LIST_URL);
httpPost2.setEntity(new StringEntity(backToString));
httpPost2.addHeader("X-RequestDigest", formDigestValue);
httpPost2.addHeader("Content-Type", "application/json;odata=verbose");
httpPost2.addHeader("Accept", "application/json;odata=verbose");
httpPost2.addHeader("X-HTTP-Method", "MERGE");
httpPost2.addHeader("IF_MATCH", "*");
CloseableHttpResponse response2 = httpClient2.execute(httpPost2, context);
Am I doing something wrong here? I am following the post request from https://dev.office.com/sharepoint/docs/sp-add-ins/working-with-lists-and-list-items-with-rest.
I need to send an ID to the server and have the server to delete one record in a DB.
I want to use the HttpDelete Apache Android SDK integrated class but I cannot figure out how to use it and how to pass parameters to the server.
With the POST request I use .setEntity method on the HttpPost class.
But in HttpDelete there's no .setEntity method.
What I have so far achieved is:
HttpClient httpclient = new DefaultHttpClient();
HttpDelete httpdelete = new HttpDelete(url);
httpdelete.setHeader(HTTP.CONTENT_TYPE, "text/xml");
response = httpclient.execute(httpdelete);
HTTP DELETE requests do not have a body. You pass parameters right on the URL:
String url = "http://foo.com/bar?bing=bang"
HttpDelete httpdelete = new HttpDelete(url);
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.
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.
There is a website with an AJAX API. I have opened Firebug to look into the details of the login HTTPS POST request.
Then I have tried to do the same POST request from my Java program using Apache HTTP Client. But somehow the server identified my request as a non browser request. It sends a security exception message, which tells me that.
When all request headers are the same, what else could identify my client as not a browser?
My guess is that it's a cookie issue (e.g. JSESSIONID the browser has stored). Include the session information with your POST. Have a look at the cookies of this site. Try disabling cookies for this site a have a look a the request again.
user-agent header? "httpclient.useragent" property
Use debug mode to see full wire logging and compare the request with firebug's one.
Dont know about the POST request but there is this for a multipart request
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
see if its of any help
EDIT: Code sample for a multipart request
String createOrderUrl = Constants.CREATE_ORDER_URL;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(createOrderUrl);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// add the information to the multipart request
entity.addPart("msisdn", new StringBody("something"));
entity.addPart("recipientname", new StringBody("something"));
entity.addPart("recipientnumber", new StringBody("something"));
entity.addPart("recipientaddress", new StringBody("something"));
// add the images
for (String imagePath : selectedImages)
{
FileBody bin = new FileBody(new File(imagePath));
entity.addPart("image", bin);
}
httpPost.setEntity(entity);
return httpClient.execute(httpPost);