Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Plugin REST API integration/Call

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi guys, 

I have a bit of a weird problem here. 

Scenario:

I need to make Calls to an external REST API from inside Dynamics 365 online. Wrote a test console app and I am able to make all the necessary calls to the external REST API.

The moment I make that a Plugin and register on D365, nothing works.

I get the below error:

“A Web exception occurred while attempting to issue the request. Unable to connect to the remote server

I contacted the third party and they confirmed that the port they are using is port 443.

Any ideas?

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin REST API integration/Call

    Hello,

    I would recommend to get in touch with API provider and ask if there is a need to whitelist IP addresses of Dynamics because the call to their API comes from it.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin REST API integration/Call

    Thanks for this, appreciated. 

    There clearly is something totally wrong here. I used HttpWebRequest/HttpWebResponse (In console) works perfectly. deploy plugin, still getting my initial error "Unable to connect to remote server"

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://apiXXXXXX.com/.../00");
    request.ContentType = "application/json";
    request.Method = "GET";
    request.Timeout = 500000;
    
    request.Headers.Add("Authorization", "Bearer Token Value");
    var response = (HttpWebResponse)request.GetResponse();
    string content = string.Empty;
    using (var stream = response.GetResponseStream())
    {
    	using (var sr = new StreamReader(stream))
    	{
    		content = sr.ReadToEnd();
    	}
    }
    tracer.Trace("Response : "   content);

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin REST API integration/Call

    No worries, I fixed the formatting.

    I remember having similar issues when I used WebClient. Check this post - stackoverflow.com/.../call-external-api-from-microsoft-dynamics-365-online

    I remember that using of HttpWebRequest/HttpWebResponse fixed the issue.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin REST API integration/Call

    Apologies, Tried formatting the code snippet, didn't win.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin REST API integration/Call

    try 
    {
    	using (WebClient client = new WebClient()) 
    	{
    		client.Headers.Add("Authorization", "Bearer Token Value");
    		client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
    		byte[] responseBytes = client.DownloadData("">api-XXXXXXXX.com/.../00");
    		string response = Encoding.UTF8.GetString(responseBytes);
    		tracer.Trace("Response : "   response); 
    	}
    } 
    catch (WebException exception) 
    { 
    	string str = string.Empty; 
    	if (exception.Response != null) 
    	{ 
    		using (StreamReader reader = new StreamReader(exception.Response.GetResponseStream())) 
    		{ 
    			str = reader.ReadToEnd(); 
    		} 
    		exception.Response.Close(); 
    	} 
    
    	if (exception.Status == WebExceptionStatus.Timeout) 
    	{ 
    		throw new InvalidPluginExecutionException( "The timeout elapsed while attempting to issue the request.", exception); 
    	}
    
    	throw new InvalidPluginExecutionException(String.Format(CultureInfo.InvariantCulture, "A Web exception occurred while attempting to issue the request. {0}: {1}", exception.Message, str), exception); 
    }

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin REST API integration/Call

    Hello,

    Can you please provide the code you use?

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans