Popular science in vernacular, understanding API in 10s

Time:2021-3-2

As a technical support engineer, Xiaopai receives many questions from customers every day. Among them, many customers will ask: “Xiaopai, is there any other way to upload cloud storage besides using the file management and FTP tools of the console?”

“Yes, you can call API interface to upload.”

“Excuse me, what is an API interface? How can I call it? “

Don’t worry, this is the API interface for you.

What is an API interface

First, let’s take a look at the definition of API interface

API (Application Programming Interface) is a set of pre-defined functions. The purpose of API is to provide application and developers with the ability to access a set of routines based on a certain software or hardware without accessing the source code or understanding the details of internal working mechanism.

From the definition,Let’s start with the key points: functions, providing applications and developers, no need to access the source code, routines. Among them, routine is a set of function interfaces or services provided by a system.

After reading this definition, do you feel lonely? There is no difference between explanation and non explanation? Well, don’t worry. Let’s have a straight talk.

In fact, API can be understood as receiving the required messenger. Through the API interface, we tell the system (routine) what we want to do, and then the system responds to the processing results.

If a real-life scenario is used as an example, the restaurant waiter is actually a typical API. Imagine that you are sitting at a table with a menu for ordering on the table in front of you. But now there is a key link missing. How can we send our list to the kitchen and then the food to my desk?

This is the time for the API. The waiter is the messenger, he will pass your request or meal, tell the information to the kitchen (system), the kitchen will know how to do, and then the food through the waiter (response) to you.

Now let’s apply this principle to a real API example.

Express number inquiry, which we should be very familiar with it. A friend sent a box of snacks to you through SF, and told you the express number, you can input the express number on SF’s official website for query. SF’s system receives the query, it will find the corresponding express information in the system and respond to you.

However, many times we do not directly through the express company’s website to query the express information. For example, for clothes bought through Taobao, the seller delivers them through SF express, but we can find the information of SF express on Taobao. So how does Taobao get the express data stored on the SF server?

That’s right. Here’s the API. Taobao can obtain the express information stored on the SF server through the API interface provided by SF, and finally display it in front of us.

Popular science in vernacular, understanding API in 10s

Now let’s look at the above definition. Is it easier to understand? API interface is a pre-defined function logic, which is provided to other system requests and then returns results.

Why use API interface

After knowing the function of API interface, the reason of using API interface is clear at a glance. For example: you are going to develop a taxi software, and you need to show the map on the page. What will you do?

If you develop maps by yourself, the development time will be greatly extended, and the cost will increase a lot. At this time, you can actually find the map API on Gaode open platform or Baidu map open platform, purchase their services, deploy and call the relevant API, so that you can quickly launch the map function in our software.

Therefore, for software providers, only by opening API and allowing other applications to call, can the software give full play to its maximum value and vitality, and at the same time, others can’t see the code and won’t hurt business secrets.

For application developers, with an open API, they can directly call the functions of many companies to do their own applications. They don’t need to do everything by themselves to save energy.

How to use API interface

Now the open API interfaces on the market are generally called through HTTP requests. For example, some weather query functions provided by the third party, we can carry the request parameters according to the API address and related documents provided by the third party, and use them directly in the browser.

Popular science in vernacular, understanding API in 10s

Popular science in vernacular, understanding API in 10s

Next, taking the upload API of youpai cloud storage as an example, Xiaopai uses curl tool to upload files to cloud storage. Next, some command-line tools and code operation will be involved. If you are not familiar with curl, you can also refer to the article before XiaopaiA combination of two swordsoh

Each API interface needs to pass different parameters according to its function. Therefore, before using an API ﹣ interface, we’d better take a look at the API description in the official document.

Popular science in vernacular, understanding API in 10s

Through the introduction of the document, we can see that if we need to request the upload interface of cloud storage, we need to use the put method to send thehttp://v0.api.upyun.comMake a request. The name of the storage service and the path to save the file are defined in the request address. And the authorization request header should be carried to ensure that the API call is legal.

The generation of authorization, small beat use the simplest basic authentication to demonstrate.

According to the introduction of authentication document, curl supports to directly pass in operator name and password to generate authentication request header.

Popular science in vernacular, understanding API in 10s

After understanding the rules of API interface call, we can start to prepare data and upload files to cloud storage space.

For example, Xiaopai has an image on her desktop. Xiaopai wants to upload the image to the images directory in her cloud storage space and save it as lufei.jpg .

Popular science in vernacular, understanding API in 10s

The cloud storage space of Xiaopai is named fileUpload upyun, the operator is named Hello, and the corresponding password is df4xhrbnpsvonu1dgdeturnchswa2z37. In this way, we have the necessary parameters to call the upload API.

Next, we will assemble the request command of curl as required.

curl -X PUT \
http://v0.api.upyun.com/fileupload-upyun/api/lufei.jpg \
-u hello:dF4XhRbnpsvonU1dgdetURncHSwa2Z37 \
-T C:\Users\hilih\Desktop\test.jpg

Parameter Description:

-X can specify how curl initiates the request

-U performs HTTP basic authentication on the incoming data, that is, Base64 encoding of the user and password group

-T can make curl read the file under the specified path

Popular science in vernacular, understanding API in 10s

In this way, we successfully called the upload API interface of cloud storage, and uploaded a local image to the storage space through the interface.

Popular science in vernacular, understanding API in 10s

However, using curl to initiate HTTP requests always seems to have some troubles, and the command line is too anti-human. Is there a more friendly interface? At that time, curl was just one of the upload methods. We could also send HTTP requests to the cloud storage API in other ways.

For example, er Gouzi’s website shares pictures. Er Gouzi processes the uploaded API and provides an API interface for his users in the form of a web page. Xiao Pai also contacted Er Gouzi and asked him to provide a simple test case.

Popular science in vernacular, understanding API in 10s

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
< title > cloud storage upload
<script></script>
</head>
<body>
Upload service space: < input ID '='bucketname' > "
Operator account: < input id ='username '>
Operator password: < input ID '='password' >
Storage path: < input id ='path '>
File selection: < input id = - fileUpload 'type = - file' name = - file '/ > < br > < br >
< button > upload
<script type="text/javascript">
window.onload = function() {
document.querySelector('button').onclick = ufload;
}
function ufload() {
var bucket = document.querySelector('#bucketname').value;
var username = document.querySelector('#username').value;
var password = document.querySelector('#password').value;
var path = document.querySelector('#path').value;
var input = document.querySelector('#fileupload');
var file = input.files[0];
axios.defaults.baseURL = 'http://v0.api.upyun.com';
axios.put('/' + bucket + path, file, {
auth: {
username: username,
password: password
}
}).then(function(response) {
console.log(response.data);
console.log(response.headers);
console.log(response.status);
if (response.status == 200) {
Alert ('upload succeeded ');
}
})
}
</script>
</body>
</html>

You are also welcome to use the familiar programming language to try to call the API upload interface.

Popular science in vernacular, understanding API in 10s

Recommended reading

“Page content cannot be accessed” may be a cross domain error!

It’s time for SSL certificate reform. Are you aware of these changes?