To create a api there are alot opporunities. Right now I prefer SOAP-APIs in Visual Studio because it is easy as possible to create such one.
I will do this with Visual Studio 2017 and a console-application, so nothing special:
For the example I googled for free SOAP-Webservice and got this one here that can handle some information about the BLZ:
To start in Visual Studio I add a reference like this:
- Rightclick in the solution explorer at references.
- Click “Add Service Reference”
- Click “Advanced…”
- Click “Add Web Reference…”
- Paste the URL and click on the arrow.
- Type in a name that specifies the webservice-name in your project.
- Click “Add Reference”.
pre>This will add a folder to your projects called “Web References” and will add the service. Like this you will also get every object and method that you need. So I am able to write this code:
var BLZService = new OpenSOAPBLZService.BLZService();
var Bank = BLZService.getBank("10070100");
Console.WriteLine(Bank.bezeichnung + " - " + Bank.ort);
Here we create a new object based on the service that we created, create a new bank object (which class is also created by the Add-Reference-Function) and show the bank description and the location of the bank in the console. So we get this out here:
If you want to work with a JSON-formatted response, there are many ways to handle this. In the following I could figure out a dynamic read that will give you the opportunity to use the different types.
The first code-example shows how to download some data with a given URL. For this you will need the assembly “System.Net”:
string url = "address?$format=json";
var webClient = new WebClient();
String jsonResponse = webClient.DownloadString(url);
The second code-example shows how to read out the single attributes. For this you have to include the assembly “Newtonsoft.Json”:
dynamic results = JsonConvert.DeserializeObject(jsonResponse);
string myName = results.Name;
string myFirstName = results.FirstName;
int myAge = results.Age;
After using the JsonConvert.DeserializeObject-function you can use every single attribute.
#This article will work with NAV 2016!
To get a JSON Response via NAV we can create a webservice-codeunit and publish it. Following this we probably use SOAP to publish the functions and will get a XML-response. In some projects it is easier to handle a JSON response. As well there are opportunities to read out a NAV response with the JSON format. The easiest way to do this is by publishing a page. To do this just go to the table “Web Service” (2000000076) and create an entry for a page. If you run this via the normal user-client (search for “web service”) you will see a Odata-link. This link is generated automatic. If you set on the Odata on the service tier and use this link you will get a response with the data of the page.
Probably it is xml-format but you can add the following at the end of you Odata-url:
Now your response should use the JSON-format.
If you want to do the same with a codeunit you will get no odata-link. This is because we use a REST-service. A REST-service is done to have the basic-handler GET, POST, PUT, … . So if you would try to write some codeunit that should provide a json-response, the codeunit had to handle all of this basic-statements. A codeunit in NAV is not made to provide data like a real REST-service. But if you want to use data that way -> publish the data with a page and you can work with this.