How to CALL Web Service (API)from NAV

So, what is an API?  

API is the acronym for application programming interface — a software intermediary that allows two applications to talk to each other.  So, with the help of API two applications can communicate and share information with each other.  

Application can communicate by Sending HTTP Requests.  

HTTP is a protocol for request-response between client and server.  

Client – The one to send the request 

Server- One who sends the response after processing the request.  

 

 

HTTP Requests 

Below listed are some types of HTTP requests.

  • GET – The GET method is to retrieve data from the server. 
  • PUT – The POST HTTP request method sends data to the server for processing. 
  • POST – The HTTP PUT method is used to completely replace a resource. 
  • DELETE – The HTTP DELETE method is self-explanatory. After execution, the resource a DELETE operation points to is removed from the server. 
  • PATCH – It allows for updates of existing resources. 

 

What are HTTP Headers? 

HTTP headers provide additional information about the data being sent in a request or response. They convey details such as content type, encoding, authentication, and caching instructions. Using headers helps in enhancing communication between the client and server, enabling better control, security, and optimization of the data exchange process. 

They convey additional information without altering the core payload (body), enabling more efficient and secure data exchange between clients and servers. 

While the request body is part of the request message carrying data from the client to the server.

What is API Authentication 

So, to communicate to the API we need to authenticate ourselves.  There are various ways to authenticate, depending on the ways permitted by the service provider.  

Examples are API Key, bearer Token (OAuth). 

Web Service API Integration in NAV  

In Navision, we can use   Codeunit -Http Web Request Mgt. , this code unit provides us with the methods to send the HTTP request.  

  1. Initialize (URL)- This is used to set the URL for the endpoint where we want to send our HTTP request.
  1. SetMethod(‘POST’); – This is used to set our method type, it may be GET, Post etc.  
  1. SetContentType(Type) – This is used to set the type of our content which we could be sending with our request. 
  1. AddBodyAsText(bodyContent) – This method is used to add our body content in the text format. 

        Further to store the response we need to have InStream , HTTP Status code, HTTP Response Headers variable with us.  

  • TEMPBLOB –> Record TempBlob  
  •  HttpStatusCode System.Net.HttpStatusCode.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′        
  •  ResponseHeaders, DataType-System.Collections.Specialized.NameValueCollection.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ 
  1. GetResponse(Instr,HttpStatusCode,ResponseHeaders)  :- This method is used to finally send our request.

Then we can convert our instream response into text and process the response as per our requirement. 

Instr.READTEXT(AccessToken); 

 An Example is shown in the below screenshot as well –  

After this point it is just that we need to read the response as per our requirement and the structure of Json response.

For more details like this please follow our company LinkedIn page where you will get all the new updates
every week:

https://www.linkedin.com/company/allgrow-technologies-pvt-ltd/mycompany/

or you can write us at contact@allgrowtech.com

Facebook
Twitter
LinkedIn

By Harshit T

Author

Leave a comment

Your email address will not be published. Required fields are marked *