2023 Release Wave 1
Check out the latest updates and new features of Dynamics 365 released from April 2023 through September 2023
Release Overview Guides and Release Plans
Dynamics 365 Release Planner
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Community
|
FastTrack Program
|
Finance and Operations TechTalks
|
Customer Engagement TechTalks
|
All TechTalks
Hello,
I'm trying to use the CRM Dynamics 2016 Web API through java to perform an update on an existing object in dynamics. So far i've had no problems with the API for gets and inserts. But the Update method keeps returning me 405 not auhtorized.
HttpURLConnection connection = null;
System.out.println("campaignId -> " + campaignId);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("(BLURRED)", 8080));
connection = (HttpURLConnection) new URL(SERVICE_ROOT_URL_IR + "campaigns(" + campaignId + ")").openConnection(proxy);
connection.setRequestProperty("X-HTTP-Method", "PATCH");
connection.setRequestMethod("POST");
connection.setRequestProperty("OData-MaxVersion", "4.0");
connection.setRequestProperty("OData-Version", "4.0");
connection.setRequestProperty("Accept", "application/json");
connection.addRequestProperty("Authorization", "Bearer " + token);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.connect();
This is the code example of the request. I then simply send the JSON object with the parameters to update.
Has anyone a solution to this problem? I am having the exact same issue with pretty much the same code approach:
String postUrl = ROOTURL + "/api/data/v8.1/accounts("+accountID+")";
URL url = new URL(postUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setRequestProperty("X-HTTP-Method", "PATCH");
connection.setRequestProperty("OData-MaxVersion", "4.0");
connection.setRequestProperty("OData-Version", "4.0");
connection.setRequestProperty("Accept", "application/json");
connection.addRequestProperty("Authorization", "Bearer " + token);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
And I am passing a JSON object to change the name and websiteurl of the account but get the 405 error. I am able to create an account but only seem to get the error when trying to update. I assume it is something to do with the "patch" method but not sure how to fix it.
It seems that the request is being sent with POST http verb instead of PATCH
POST is only supported on entityset e.g. api/data/v8.2/accounts
if you make POST on entity record (e.g. api/data/v8.2/accounts(accountid) , then the service with throw 405-Method not allowed.
Please use PATCH api/data/v8.2/accounts(accountid) to update the record.
We did use PATCH - still received the error. I opened a MS Support Case in order to resolve the issue.
Just by using the overriding the method wouldn't work... connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
You also need to call a separate function to like below within your update request.
allowMethods("PATCH");
private static void allowMethods(String... methods) {
try {
Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
methodsField.setAccessible(true);
String[] oldMethods = (String[]) methodsField.get(null);
Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
methodsSet.addAll(Arrays.asList(methods));
String[] newMethods = methodsSet.toArray(new String[0]);
methodsField.set(null/*static field*/, newMethods);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
I'd suggest you should use CRM Rest Builder tool for creating REST api requests to CRM. Its hassle free with nice GUI.
CRM REST Builder
It seems that the request is being sent with POST http verb instead of PATCH
POST is only supported on entityset e.g. api/data/v8.2/accounts
if you make POST on entity record (e.g. api/data/v8.2/accounts(accountid) , then the service with throw 405-Method not allowed.
Please use PATCH api/data/v8.2/accounts(accountid) to update the record.
We did use PATCH - still received the error. I opened a MS Support Case in order to resolve the issue.
Just by using the overriding the method wouldn't work... connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
You also need to call a separate function to like below within your update request.
allowMethods("PATCH");
private static void allowMethods(String... methods) {
try {
Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
methodsField.setAccessible(true);
String[] oldMethods = (String[]) methodsField.get(null);
Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
methodsSet.addAll(Arrays.asList(methods));
String[] newMethods = methodsSet.toArray(new String[0]);
methodsField.set(null/*static field*/, newMethods);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
I'd suggest you should use CRM Rest Builder tool for creating REST api requests to CRM. Its hassle free with nice GUI.
CRM REST Builder