web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics CRM 2016 Web API update Request (405 Method not Allowed)

(0) ShareShare
ReportReport
Posted on by

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. 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi,

    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.

  • Vidyuth Profile Picture
    55 on at

    Hello,

    Is there a solution to this issue? We have a similar issue where we are trying to update a Lead in MSD CRM from java application with the exact similar code like above and we are receiving a 405 error. How to resolve this??

  • Suggested answer
    Community Member Profile Picture
    on at

    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.

  • Suggested answer
    Vidyuth Profile Picture
    55 on at

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

  • Suggested answer
    imayur Profile Picture
    630 on at

    Hi,

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans