What is a Web API?

 what is a Web API? before we dive into this, let's define what an API is. API stands for Application programming interface and is an interface that provides a set of functions to a user where the underlying mechanism of that function is hitting a good real world. An example of this is an elevator and the elevator provide you with an interface in the form of a set of buttons. You press one of those buttons and the elevator takes you to the floor. You don't really care how the elevator is moving up and down. You just care that once you push that button, it takes you to the correct floor and the door opens.



 There are many different types of APIs operating system that provides an API to the applications that are running on them. For example, open is a function provided by the operating system that an application can use so when you're using an application and you try to open a file, what's actually happening is your application is asking the operating system to go open that file for you.

Other functions could be killed, for example when you close an application, you're calling the kill function on that application. You can also call reboot, to reboot the entire system.

So that brings us back to Web API. A Web API is basically just an API that travels through the Internet. You have the client who is the caller of the API and the server who is the respondent. This here is a client-server model. The client makes a request to a server that gets routed through the Internet, the server receives the request and performs whatever action was requested by the client. The server will have several endpoints set up that perform different actions these actions could be something like retrieving data from a database, posting new data, or deleting existing data. Once the server performs whatever it needs to do on it, send, it returns the response back to the client daily to it gets pass around through the web using a protocol called HTTP, which stands for Hypertext Transfer protocol.

 How do you send an HTTP request? You can use a tool like your standard Chrome browser and an example of this request can look like the following.

 https://www.anythingprogramming.com

It starts with the protocol, so your browser knows what protocol to use. You need to provide a host so it knows where to route. The request then you need the path. So once you reach their server it knows what action to perform. You could also have an optional query. Say your request is creating a new user and you want to provide the name for that user. You would provide it as part of the query string, and that's what a standard HTTP request looks like this should look very familiar because whenever you type a URL into your browser. You're calling a Web API.

Now when you get a response from the server, there will be a status code associated with that response. This lets the client know how the request went. If everything went as expected, there will be a status code in the 200 range. However, you could also receive a status code in the 400 range. This means that something went wrong, once the client created the request. For example, if the path doesn't exist, you would receive 404 error statuses that can also be in the 500 range, meaning that something went wrong on the server-side.

Now, what does the data that gets sent over look like? Well, the two main formats that are used are XML and Jason. XML stands for Extensible Markup Language, and as you can see it looks very much like HTML, but the tags are defined by the developer.

In our example here we're passing 3 fields, name, age and salary, so the client would send this data in the body of the HTTP requests and the server would then know how to parse it out and get the appropriate data. Likewise, we have Jason or JavaScript object notation which has a different format, but the purpose is exactly the same to transmit data from the client to the server.

So which one should you use? Jason is usually the preferred format because a lot of times when you're making the API call using JavaScript, which integrates well with Jason, it's easier to read and has a smaller size because we don't have those open and close tags that you get in XML. Most modern API's use Jason but some older ones may use XML, so you should be familiar with both. Now the client and server need to have an agreed-upon way to send these requests so they can understand one another.

 One popular way is to use rest, which stands for the representational state, a service implementing the rest architecture is called a restful service.

Rest provides interoperability between a client and a server, and it is defined by its architectural style. So what is a rest call consists of?

 Well, you first have the endpoint. This would be just like the HTTP path that we mentioned in the previous module. Next is the method. This defines what kind of action you're making. A few examples are a get post and delete, which will take a look at, in a moment you have the header headers contain information about the type of requests. For example, are we using a Jason or XML format? Finally, we have the body. This would be something like a Jason object. So let's take a look at an example. Here I'm using postman, which is a tool you can use to make API calls very similar to the browser.

Conclusion

This is implemented using some kind of back end programming languages like Java or Python. All your major programming languages are going to have some kind of framework for standing up an API. For example, if you're using Java, you can use spring. If you're using Python, you can use Django. If you're using JavaScript, you can use express. 

If you find this piece informative share and comment below.

Elliot

Elliot is a student of the University of Energy and Natural Resources (UENR), a frontend web developer and owner of anythingprogramming. Elliot is a tech-inclined person who loves to share his knowledge with others and also learn from others as well. He loves to write and so anythingprogramming came to life.

Post a Comment

Previous Post Next Post