Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

HTTP requests to On-Premise Web API

Posted on by Microsoft Employee

Hello,

I've seen a number of questions posted about how to execute Web API requests via HTTP external to the CRM, but I still am very unclear how to do this. It's rather frustrating that I can simply navigate to the ".../api/data/v8.0/" end point in my browser, but can't seem to access this via any other method. In PowerShell, I've tried the following code:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", 'application/json')
$headers.Add("OData-MaxVersion", '4.0')
$headers.Add("OData-Version", '4.0')

Invoke-WebRequest -Uri 'crmtest.regent.edu/.../v8.0' -Headers $headers -Credential Get-Credential

But I get this error:

Invoke-WebRequest : The underlying connection was closed: An unexpected error 
occurred on a send.
At line:6 char:1
+ Invoke-WebRequest -Uri 
'crmtest.regent.edu/.../v8.0 ...
+ 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpW 
   ebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell 
   .Commands.InvokeWebRequestCommand


I've tried the Invoke-RestMethod as well. Similar requests in JavaScript don't seem to yield requests either.

What am I doing wrong?

Many thanks,

Daniel Hines

Regent University

Christian Leadership to Change the World

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HTTP requests to On-Premise Web API

    Thank you for the links. Is this something I can configure on an on-premise instance of Dynamics CRM 2016 (not IFD)?

  • Suggested answer
    joman Profile Picture
    joman 617 on at
    RE: HTTP requests to On-Premise Web API

    Good day. You make cross-domain request, so browser don't do it.

    You need to make a server, witch you are sending a request, give you a cookie, that allow cross-domain requests.

    More info:

    developer.mozilla.org/.../Access_control_CORS

    or google: Access-Control-Allow-Origin

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HTTP requests to On-Premise Web API

    Thank you both for the responses. I was able to working in Powershell; however, I haven't been able to do so in Javascript.

    $(function () {
      $.ajax
        ({
          type: "GET",
          url: "rctrdevcrm.regent.edu/.../v8.0",
          dataType: 'json',
          async: false,
          headers: {
            "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
          },
          success: function (res) {
            console.dir(res);
          },
        }).fail(function (e) {
          console.dir(e);
         })
    });

    Here's the error I'm getting: 


    XMLHttpRequest cannot load rctrdevcrm.regent.edu/.../v8.0. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access.

    Why can I navigate straight to the page in the browser, or access the data with PowerShell, Power BI, etc., but not via XMLHttpRequest?

    Thanks in advance,

    Daniel Hines

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: HTTP requests to On-Premise Web API

    Hi

    This (Invoke-WebRequest -Uri 'crmtest....) should work.

    Please check the URL (Example: crmtest.regent.edu/.../accounts) in the browser.

  • joman Profile Picture
    joman 617 on at
    RE: HTTP requests to On-Premise Web API

    First of all, check your URL in CRM -> Parameters -> Settings -> Resources for developers.

    Then check this: msdn.microsoft.com/.../gg334279.aspx

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans