In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to quickly understand the API interface", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to quickly understand the API interface"!
What is the API interface
First, let's take a look at the definition of the API interface:
API (Application Programming Interface, Application programming Interface) are predefined functions designed to provide applications and developers with the ability to access a set of routines based on some software or hardware without having to access the source code or understand the details of the internal working mechanism.
From the definition, we first draw the focus: functions, providing applications and developers, no need to access source code, routines. Among them, a routine is a collection of functional interfaces or services provided by a system.
After reading this definition, do you feel lonely? this explanation is no different from whether or not to explain. All right, don't worry, Xiao Pai will tell you directly.
API can actually be understood as receiving the required messenger. Through the API interface, we tell the system (routine) what we want to do, and the system responds back to the processing result.
If you use a real-life scenario to illustrate, then the restaurant waiter is actually a typical API. Imagine you are sitting at the dinner table with a menu for ordering on the table in front of you. After ordering, it will be handled by the kitchen. But now that there is a key link missing, how can we send our list to the kitchen and the food to my table?
This is the time for the API to take the stage. The waiter is the messenger who will pass on your request or meal, tell the kitchen (system), and the kitchen will know what to do, and then send the meal to you through the waiter (response).
Now let's apply this principle to a real API example.
Express order number inquiry, this should be very familiar with it. A friend sent you a box of snacks through Shunfeng, and told you the express order number, you can enter the express order number on the official website of Shunfeng to inquire. When Shunfeng's system receives the query, it will find the corresponding express information in the system and respond to you.
However, most of the time we do not directly query the express information through the website of the express company. For example, through the clothes bought on Taobao, the seller delivers the goods through Shun Feng, but we can query the information of SF Express on Taobao. So how does Taobao get the express data stored on Shun Feng server?
Yes, API is used here. Taobao can get the express information stored on the SF server through the API interface provided by SF, and finally display it in front of us.
Now if we look at the definition above, does it feel better to understand? The API interface is a predefined function logic that is provided to other system requests and then returns the result.
Why use the API interface
After knowing the function of API interface, the reason for using API interface is clear at a glance. For example: if you plan to develop a taxi-hailing app that needs to display a map on the page, what will you do?
If you develop the map yourself, the development time will be greatly extended and the cost will increase a lot. At this time, you can actually find map API on Gaode open platform or Baidu map open platform, purchase their services, deploy and call relevant API, so that you can quickly launch the map function in our software.
Therefore, for software providers, open API and let other applications to call, form an ecology, the software can play the greatest value, can be more vitality, at the same time others can not see the code, will not hurt trade secrets.
For application developers, with an open API, they can directly call the functions done by many companies to do their own applications, without having to do everything on their own and save energy.
How to use the API interface
The API interfaces that are now open in the market are generally called through HTTP requests. For example, for the weather query function provided by some third parties, we can directly access and use the request parameters in the browser according to the API address provided by the third party and the relevant documentation.
Next, Xiaopai takes re-shooting cloud storage to upload API as an example, and uses the curl tool to complete an operation of uploading files to cloud storage. The following will involve some command-line tools and code operations, if you are not familiar with the tool curl, you can also refer to the previous article "combination of two swords-mastering curl and Dig".
According to the function of each API interface, the parameters to be passed are different. Therefore, it is best to take a look at the API description of the official documentation before using an API interface.
Through the documentation, we can see that if we need to request the upload API of cloud storage, we need to use PUT to initiate a request to http://v0.api.upyun.com. The storage service name and the save path of the file are defined in the request address. And you need to carry the Authorization authentication request header to ensure that the call to API is legal.
For the generation of Authorization, small racquets use the simplest basic authentication to demonstrate.
According to the authentication document, curl supports passing the operator name and password directly to generate the authentication request header.
Once we understand the rules for calling the API interface, we can begin to prepare data for uploading files to the cloud storage space.
For example, there is a picture on the desktop of Xiaopai. Xiaopai wants to upload the picture to the images directory under its cloud storage space and save it as lufei.jpg.
The cloud storage space of Xiaopai is fileupload-upyun, the operator is hello, and the corresponding password is dF4XhRbnpsvonU1dgdetURncHSwa2Z37. In this way, we have the necessary parameters to call upload API.
Next, we assemble the request commands for 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 the method by which curl initiates the request
-u performs basic HTTP authentication on the incoming data, that is, the base64 encoding of the user and password group
-T enables curl to read files under a specified path
In this way, we successfully call the upload API API of Cloud Storage and upload a local image to the storage space through the API.
However, there always seems to be some trouble in making HTTP requests using curl, and the command line is too anti-human, isn't it? is there a more friendly interface? At that time, using curl is only one of the upload methods, we can also use other ways to initiate HTTP requests to cloud storage API.
For example, when Ergouzi shares pictures on his website, Ergouzi handles the uploaded API and provides an uploaded API interface to his users in the form of a web page. Xiao Pai also contacted er Gouzi and asked him to provide a simple test case.
Cloud storage upload service space: operator account: operator password: storage path: file selection:
Upload _ 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 successful') }})} at this point, I believe you have a deeper understanding of "how to quickly understand the API interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.