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)

PATCH request

(0) ShareShare
ReportReport
Posted on by

I'm writing a plug-in to send a PATCH request to the REST API whenever a new opportunity is created. But I keep getting the same error "400 Bad Request". However, I tested the URL in POSTMAN and it works fine. This is my code (the plug-in will call this method):

private async void MakeRequest(string requestUrl, ITracingService tracingService)
        {
            try
            {
                HttpClient client = new HttpClient();

                client.BaseAddress = new Uri(requestUrl);
                client.DefaultRequestHeaders
                    .Accept
                    .Add(new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", subscriptionKey);

                HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), "relativeAddress");
                request.Content = new StringContent("{\"name\":\"John Doe\",\"path\":\"prescriptive-actions/status\",\"value\":\"Completed\",\"from\":\"New\"}", Encoding.UTF8, "application/json");

                var task = client.SendAsync(request)
                    .ContinueWith(responseTask =>
                    {
                        tracingService.Trace("responseTask {0}", responseTask.Result);
                    });

                task.Wait();
            }
            catch (Exception ex)
            {
                tracingService.Trace("PROSPluginNotifyPAO Error: {0}", ex.ToString());
                throw;
            }
        }

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Bas van de Sande Profile Picture
    4,383 on at

    Hi

    the first thing that crosses my mind is that you need to specify some additional headers in which you indicate that it is a json call.

    I assume you verified the incoming requesturl. is it a valid url (crm sandbox does not allow calling ip addresses directly)?

    are you running an onpremise crm or crm online?  In case you are running on  premise, have you verified (by using remote desktop) on the server that you can reach the destination url?

    Looking forward to hear from you

    Bas

  • Community Member Profile Picture
    on at

    Hi,

    Thanks for your response.

    I already added "new MediaTypeWithQualityHeaderValue("application/json")". Isn't this sufficient?

    Yes, it's a valid URL. The URL is something like prospaodev.azure-api.net/.../678391. It's not an IP address.

    This is CRM Online 2015.

    Hope to hear back from you.

  • Verified answer
    Bas van de Sande Profile Picture
    4,383 on at

    ws_5F00_magic.png

    Hi

    I would try to rewrite your code like this. These are the headers I usually send. (I took it from an example on my blog --> http://journeyintocrm.com/archives/1209)

    Furthermore, what you can do to debug is to write a simple console app, in which you test the communication part. Once that works, you can implement it in the plugin. Keep in mind that on CRM online your code runs in a sandbox. Some of the Microsoft namespaces (like System.Web.Extensions) give strange errors.

    I would advise you in the first place, to test the communications in a standalone console app.

    Please let me know if I can help

    Bas

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