Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

How to call external API from navision

Posted on by 2,576

Hi all,

I want to call external api from navision

I have used below code

CallRESTWebServiceFinal(BaseUrl : Text;Method : Text;RestMethod : Text;HttpContent : DotNet "System.Net.Http.HttpContent";HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage")

HttpClient := HttpClient.HttpClient();

HttpClient.DefaultRequestHeaders.Add('Username', 'username');
HttpClient.DefaultRequestHeaders.Add('Password', 'password');
HttpClient.DefaultRequestHeaders.Add('ContentType', 'application/json');
HttpClient.BaseAddress := Uri.Uri(BaseUrl);
CASE RestMethod OF
'GET':
HttpResponseMessage := HttpClient.GetAsync(Method).Result;
'POST':
HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result;
'PUT':
HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result;
'DELETE':
HttpResponseMessage := HttpClient.DeleteAsync(Method).Result;
END;

HttpResponseMessage.EnsureSuccessStatusCode(); // Throws an error when no success

CallAPIFunction()

SubUrl:='suburl';
ServicePointManager.SecurityProtocol:=SecurityProtocolType.Tls12;
CallRESTWebServiceFinal('MainURL',SubUrl,'GET',null,HttpResponseMessage);
result := HttpResponseMessage.Content.ReadAsStringAsync.Result;

But I'm getting below error

unsupported media type

please help

  • Tina Menezes Profile Picture
    Tina Menezes 2,576 on at
    RE: How to call external API from navision

    Hello Sir,

    Yes, its working with postman.

    Even after putting the code which you suggested I'm getting same error.

    HttpClient.DefaultRequestHeaders.Add('Accept', 'application/json');

    HttpClient.DefaultRequestHeaders.Add('ContentType', 'application/json');

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: How to call external API from navision

    I would suggest first the URL result with the Postman App or any other app, if you are getting the same result, the same you want to achieve with this code in NAV

    Also try to add below lines

    HttpClient.Accept := 'application/json';
    HttpClient.ContentType := 'application/json';

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans