YTread Logo
YTread Logo

REST API concepts and examples

Jun 01, 2021
welcome today I want to talk about what an API is and why it is important in web development API stands for application programming interface and it is basically something that allows one piece of software to communicate with another. Now there are many different types of APIs, but when you hear people talk about the Twitter API or the Google API, what they're talking about is a

rest

API and a

rest

API means representational state transfer. Now it doesn't have to be this way, but generally a rest API works more or less the same way as a website.
rest api concepts and examples
Do you make a call from a client to a server and get data over the HTTP protocol? I think one of the best ways to show the many similarities between a resting API call and loading a normal web page can be found in the Facebook graph. API, for example, let's open the Facebook www.fb.com from YouTube youube and we are all familiar with what a Facebook page looks like. It shows how many likes the YouTube page has, things like that, but now let's change the www part to graph. face.com youube and what we receive is a response to our API request.
rest api concepts and examples

More Interesting Facts About,

rest api concepts and examples...

We have made an API request in our browser to Facebook's scph API. Now what we receive may look like gibberish to the human eye, but it is actually JavaScript object notation. or Json formatted data, is structured data organized by key-value pairs in the same way that an Excel spreadsheet is structured by key-value pairs and can request the data that is in cell A6. You can query the data to a Json array if you want to know how many likes this Facebook page had for the data contained in the Likes key and all modern programming languages ​​will be able to interpret this Json response.
rest api concepts and examples
One more concept I want to introduce is the Parameters IDE. Let's reload that same Facebook API request, but this time. let's add fields equal to id name and like. Now when we refresh the page you will see that only the ID name and likes have been returned, that is because these parameters have filtered the data we get from this response. Now let's look at another API example. What I think is really cool is provided by Google Maps and allows you to take the name of a city or even an address and convert it into a set of GPS coordinates. guys.com Maps API geocodes Json, so if you remember what we just talked about, The server we call is Maps. googleapis.com and then the particular resource that we have drilled down to is the Maps resource and then the API resource and then the geoc code resource and we even added Json as a resource and that's because the Google Maps API can return data in a number of different formats and then we will add the following parameters.
rest api concepts and examples
We'll add address equals Chicago and sensor equals false and as you can see we got another Json response and if we look in the Json array and go to the key results and then the key geometry and then the key location, we see the latitude and longitude coordinates of the city of Chicago. Now that's cool and even though we have the geolocation coordinates for Chicago, what do we do with them? Well, let's put two APIs together and take those geographic coordinates. coordinate with Instagram and connect them to the Instagram media search point, so to access the Instagram media search point without having to write any code, we will go to the instagram.com developers and then on the left side , we will click on API. console and we will be able to use this API console provided by apig or rige to make requests to the Instagram API without needing to write any code and you can see if you click on the dropdown on the left, there are a whole bunch of APIs with which this is set up to allow you to play, but for now we'll use Instagram and we'll use the Instagram media search dot that you can see is in the API. instagram.com slv1 V1 medas search and then we will set the following parameters and you can see they are added to our URL request at the top of the screen, we will set lat to 41.87 and run length equal to 8762 and set distance to 20M, We click Submit and we get this information about our request.
We see that it is a git request and it shows where it was submitted and what parameters we passed in one of the parameters. What we didn't configure was the access token because it was automatically configured by the aige uh interface and then below that, in sort of blue and purple, you have key-value header pairs that we send as part of our request and then if you look the response and you can see that we have an HTTP status code of 200 which means everything went well and then there is header information as well as a rate limit x which is 5000 which is the total number of requests you can make to the Instagram API using an access token for a rate cap period and then below you can see the remaining rate cap rate and then there's information about the cookies and the Json content type application, things like that and then if we scroll down we'll see the body of the response which is a Jason coded array of all the images that match the geographic coordinates that we pass in and there's information like as you know, what filter was used to take the Instagram photo, how many likes it gets, how many comments, information about the user who posted it, how many followers they have, things like that and then of course, There are also the images themselves and if we copy them. that image link and we take it to our U browser and paste it, then you can see there is an image of that location in Chicago, you can even see the Chicago skyline in the background, very cool, there are literally thousands of APIs out there that you can go in and take your data and combine it, pass it to another API, chances are any website you want to work with has some kind of API that you can use to consume their data APIs that are available.
See the programmable web. .com so far we have only been consuming data from apis, but you can also write data to apis, but before we go down that path we need to talk a little about the concept of HTTP request methods. I've linked to the documentation in the description below, but the two big ones you really need to know are get and publish now. A get request is what you use to consume data and that's what you've seen us do so far when passing these URL parameters to retrieve data. from the API, but a subsequent request if we are writing data to the API, the best practice is to put the data in the body of the request, now a normal web browser does not allow you to put data in the body of a request, but what What you can do is install this handy extension called Postman rest client.
The nice thing about working with Postman is that you can make more complex API requests, for example you can choose any of the available HTTP request methods and you can see a list of them here, secondly you can add a body to your HTTP request and Finally, you can add headers. Let's use the postman client to send a tweet via the Twitter API before doing so, although obviously let's briefly talk about the concept of authentication. You need to provide some kind of authentication if you're going to tweet because otherwise you could tweet from my Twitter account and I can tweet from yours, and what a lot of these big online websites use for their authentication is what is known as ooth or ooth 2, let me tell you briefly, basically what you are doing is getting credentials like a username and password, although they are called client ID and client secret, and then you exchange them. for what is known as an access token and then you pass that access token to Twitter and Twitter knows that the request to make the Tweet is coming from you, so it sends the tweet from your Twitter account.
Okay, let's go ahead and submit a proof. tweet using the postman rest client, we will select post as our HTTP request method and then you will see that the server we are accessing is in api. twitter.com and then as we delve deeper into our tweeting resource, we get to Twitter API version 1.1 and then the SL status update. Json and again that tells the Twitter API that we want data formatted by Jason as a response and we set the authorization header to our oo 1.0 credentials and then in the body of our post request we set the key state to test the tweet. from Postman and when we click Send, you will see that there is information about what time the Tweet was created, the ID of the Tweet that was assigned information about my Twitter account or the Twitter account from which the Tweet was sent and then if we click on my timeline and refresh the page, you'll see for sure that the test tweet we sent appears n

If you have any copyright issue, please Contact