Column

Programming
[HttpGet] Running API communications
2023.02.14

This column specifies how we can write an API for communication between angular and C#. After reading this column you will be successfully able to write a Get API. We will look at an example to understand how they work.

public class FrameController : ControllerBase
{
 [HttpGet]
 public ActionResult Get()
 {
   var variable1 = Manager.Frames();
   var ByteFrames = Encoding.ASCII.GetBytes(variable1)
   return this.File(ByteFrames,"application/octet-stream");
 }
}

The above is a simple example of HttpGet protocol which sends information to the frontend of the application about a variable called “Frames” set in the Manager class. It is important to note that when using such APIs we would convert the type of the variable to byte. As the API sends the information in terms of a byte packet or file stream. In the frontend a function would specifically call for this API to call for the information it provides. As an important side note: we need to add the name of the class to proxy.conf.js for it to work as intended.

var url4 = this.baseUrl + 'frames'; //for frames
var arrayBufPromise2 = this.getArrayBuffer(url4);
this.TotalFrames = arrayBufPromise2.then((res) => {
var bytearray = DatastoreService.ToUint8Values(res); 
return bytearray;
})

The Frontend snippet would look like this, as it calls the API and asks for a response. This response is converted within the function to its original type at the start of this process with a different function embedded in the datastore class.

So, this will be our short introduction to APIs and how we can make a simple API to have the frontend and backend communicate.  We will add a similar column about Post API protocol as well so stay tuned!