带有PATCH请求的Dynamics CRM插件

时间:2015-10-06 23:16:39

标签: dynamics-crm microsoft-dynamics

我正在编写一个插件,以便在创建新机会时向REST API发送PATCH请求。但我一直得到相同的错误“400 Bad Request”。但是,我在POSTMAN中测试了URL,它运行正常。这是我的代码(插件将调用此方法):

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("{\"op\":\"replace\",\"path\":\"prescriptive-actions/status\",\"value\":\"Completed\",\"from\":\"New\"}", Encoding.UTF8, "application/json");

            tracingService.Trace("requestUrl {0}", requestUrl);

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

更新1 这适用于CRM Online 2015.以下是堆栈跟踪

requestUrl http://prospaodev.azure-api.net/hpho/prescriptive-actions/678391
    responseTask StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Date: Tue, 06 Oct 2015 23:04:11 GMT
    Content-Length: 0
    }

0 个答案:

没有答案