
什么是 REST API?
特點
適用場景
示例
<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"">
<SOAP-ENV:Body>
<m:GetPrice xmlns:m=""http://example.org/stock"">
<m:StockName>IBM</m:StockName>
</m:GetPrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
特點
適用場景
示例
GET /api/products HTTP/1.1
Host: example.com
Accept: application/json
特點
適用場景
示例
query {
user(id: ""123"") {
name
profilePic
friends {
name
}
}
}
特點
適用場景
示例
syntax = ""proto3"";
package example;
service ExampleService {
rpc GetExample (ExampleRequest) returns (ExampleResponse);
}
message ExampleRequest {
string id = 1;
}
message ExampleResponse {
string name = 1;
}
特點
適用場景
示例
const socket = new WebSocket('ws://example.com/socket');
socket.onmessage = function(event) {
console.log('Message from server:', event.data);
};
特點
適用場景
示例
POST /webhook HTTP/1.1
Host: example.com
Content-Type: application/json
{
""event_type"": ""push"",
""repository"": {
""name"": ""example-repo""
}
}
以上就是目前最常用的六種 API 架構風格。正如我們所見,沒有一種架構風格是萬能的。每種風格都有其適用場景和優缺點。根據你的項目需求選擇合適的架構風格,將有助于你構建更高效、更可靠的系統。
原文引自YouTube視頻:https://www.youtube.com/watch?v=4vLxWqE94l4