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.
Related
I'm trying to upgrade some Java code to fit in with an android app.
The code references the depreciated the Apache http module which Android no longer supports since Marshmellow.
I need to complete a POST of the XML code.
I'm new to connections and OKHttp3. Is there a way I can achieve a similar result with using OKHttp3?
private static String SubmitXml(String Xml, String Url) throws Exception {
HttpClient client = new DefaultHttpClient();
// Prepare the POST request
HttpPost pxpayRequest = new HttpPost(Url);
pxpayRequest.setEntity(new StringEntity(Xml));
// Execute the request and extract the response
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(pxpayRequest, responseHandler);
return responseBody;
}
I've searched the web but most OKHttp3 articles I've seen reference sending JSON data or something similar. Nothing with XML.
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.
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.