REST API คืออะไร

ในบทช่วยสอนนี้ คุณจะได้เรียนรู้พื้นฐานของ REST API, REST API คืออะไร, ทำงานอย่างไร และอะไรที่ทำให้ API RESTful

REST APIs: คืออะไรและทำงานอย่างไร และทำให้ API RESTful

API (ย่อมาจาก Application Programming Interface) ช่วยให้สองแอปพลิเคชันขึ้นไปสื่อสารกันเองและส่งข้อมูลไปมาได้

API ดำเนินการตามกฎมาตรฐานและเป็นองค์ประกอบสำคัญของการพัฒนาซอฟต์แวร์สมัยใหม่

มีรูปแบบ API ที่แตกต่างกัน และแต่ละรูปแบบมีสถาปัตยกรรมที่เป็นเอกลักษณ์ หนึ่งในสไตล์ที่พบมากที่สุดคือ REST

ในบทความนี้ คุณจะได้เรียนรู้พื้นฐานของ REST API คุณจะเห็นภาพรวมว่ามันคืออะไรและทำงานอย่างไร คุณจะได้เรียนรู้สิ่งที่ทำให้ API RESTful

เข้าเรื่องกันเลย!


REST API คืออะไร?

REST (ย่อมาจาก REpresentational State Transfer) เป็นรูปแบบสถาปัตยกรรมซอฟต์แวร์ที่สร้างขึ้นโดยนักวิทยาศาสตร์คอมพิวเตอร์ Roy Fielding ในปี 2000

ด้วย REST APIs ลูกค้าร้องขอทรัพยากร จากนั้นเซิร์ฟเวอร์จะตอบกลับลูกค้าด้วยการแสดงสถานะปัจจุบันของทรัพยากรนั้นและข้อมูลที่เกี่ยวข้องทั้งหมดในรูปแบบมาตรฐาน เช่น JSON หรือ XML

ภาพรวมของแนวคิด REST API

ในส่วนก่อนหน้านี้ ฉันได้กล่าวถึงคำว่าไคลเอนต์ ทรัพยากร และเซิร์ฟเวอร์ พวกเขาหมายถึงอะไร?

ลูกค้าคือโปรแกรมที่ทำงานบนคอมพิวเตอร์ของผู้ใช้

โปรแกรมนี้อาจเป็นเว็บเบราว์เซอร์ที่เริ่มต้นการสื่อสารและทำการร้องขอไปยัง API

บทบาทของไคลเอ็นต์คือการดึงหรือแก้ไขข้อมูลภายในแอปพลิเคชันเท่านั้น

ทรัพยากรคือข้อมูลใดๆ ที่ API สามารถให้ได้ – เอกสาร ไฟล์ข้อความ รูปภาพ วิดีโอ หรือวัตถุอื่นใด

ทรัพยากรแต่ละรายการมีตัวระบุที่ไม่ซ้ำกัน ซึ่งเรียกว่า Universal Resource Identifier (หรือ URI) ซึ่งเป็นสตริงที่อ้างถึงทรัพยากรเฉพาะนั้น

เซิร์ฟเวอร์ คือระบบที่เก็บ ทรัพยากรที่ลูกค้าร้องขอ

บทบาทของเซิร์ฟเวอร์คือการรับฟังและรับคำขอของไคลเอนต์สำหรับทรัพยากร และตอบกลับด้วยข้อมูลที่ร้องขอ

เซิร์ฟเวอร์ส่งการแสดงสถานะของทรัพยากรกลับคืนและไม่ให้สิทธิ์การเข้าถึงแบบเต็มแก่ไคลเอนต์

REST API ทำงานอย่างไร

ลูกค้าส่งคำขอ HTTP ไปยังเซิร์ฟเวอร์เพื่อดำเนินการ CRUD CRUD เป็นตัวย่อของCreate, Read, Update, Deleteและ

เซิร์ฟเวอร์ตอบสนองด้วยทรัพยากรในรูปแบบมาตรฐาน เช่น JSON (JavaScript Object Notation) หรือ XML โดยที่ JSON เป็นรูปแบบที่นิยมใช้กันมากที่สุดในปัจจุบัน

ภาพรวมของคำขอ REST API

คำขอ REST API จำเป็นต้องมีสิ่งต่อไปนี้:

  • การดำเนินการ
  • จุดสิ้นสุด
  • พารามิเตอร์/เนื้อหา
  • ส่วนหัว HTTP

ประการแรก คำขอแต่ละรายการจำเป็นต้องรวมการดำเนินการ/การดำเนินการที่คุณต้องการให้เซิร์ฟเวอร์ดำเนินการกับทรัพยากร

ในส่วนด้านบน ฉันได้กล่าวถึงว่าไคลเอนต์ส่งคำขอไปยังเซิร์ฟเวอร์เพื่อดำเนินการ CRUD

การดำเนินการ CRUD แต่ละครั้งจะเทียบเท่ากับเมธอด/กริยา HTTP ที่กำหนดการดำเนินการ:

  • การCreateดำเนินการที่เทียบเท่าคือPOSTคำขอซึ่งระบุว่าคุณต้องการสร้างทรัพยากรใหม่
  • การReadดำเนินการที่เทียบเท่าคือGETคำขอซึ่งระบุว่าคุณต้องการดึงข้อมูลทรัพยากร
  • สิ่งUpdateที่เทียบเท่าของการดำเนินการคือPUTคำขอซึ่งระบุว่าคุณต้องการแก้ไขหรืออัปเดตทรัพยากรที่มีอยู่
  • การDeleteดำเนินการที่เทียบเท่าคือDELETEคำขอซึ่งระบุว่าคุณต้องการลบทรัพยากรเฉพาะ

จากนั้น คำขอจะต้องมีปลายทางด้วย

จุดสิ้นสุดคือ URL (ย่อมาจาก Uniform Resource Locator) ที่มี URI (Uniform Resource Identifier) ​​ซึ่งเป็นตำแหน่งที่ API สามารถค้นหาและโต้ตอบกับทรัพยากรเฉพาะได้

คุณสามารถดูรายการปลายทางได้ในเอกสารประกอบสำหรับ API เฉพาะที่คุณกำลังใช้

ตัวอย่างเช่น เมื่อใช้API ของ Spotifyจุดสิ้นสุดในการรับอัลบั้มของศิลปินที่เฉพาะเจาะจงจะมีลักษณะดังนี้:

https://api.spotify.com/v1/artists/{id}/albums

นอกจากนี้ คำขอยังมีพารามิเตอร์/bodyซึ่งเป็นข้อมูลและข้อความเพิ่มเติมที่คุณอาจส่งไปยังเซิร์ฟเวอร์

ประการสุดท้าย คำขอยังมีส่วนหัว HTTP ซึ่งเป็นข้อมูลการตรวจสอบสิทธิ์ เช่น คีย์ API

ภาพรวมของการตอบสนอง REST API

หลังจากได้รับการร้องขอ เซิร์ฟเวอร์จะส่งคืนข้อมูลเกี่ยวกับทรัพยากรที่ร้องขอ

การตอบสนองคือการแสดงสถานะของทรัพยากรที่ร้องขอ ไม่ใช่ตัวทรัพยากรเอง

โดยทั่วไปแล้ว การตอบสนองจะอยู่ในรูปแบบของข้อมูล JSON ซึ่งเป็นรูปแบบที่อ่านได้สำหรับไคลเอนต์ที่ส่งคำขอ

นอกจากนี้ยังมีรหัสสถานะ HTTP ที่เหมาะสมที่ส่งกลับในส่วนหัวการตอบสนองเพื่อให้ไคลเอนต์ทราบว่าการดำเนินการสำเร็จหรือไม่

รหัสสถานะที่ใช้บ่อยที่สุดบางส่วนมีดังต่อไปนี้:

  • 200 OKหมายถึงคำขอสำเร็จ
  • 201 Createdหมายความว่าคำขอสำเร็จ และสร้างทรัพยากรแล้ว
  • 204 No Contentหมายความว่าคำขอสำเร็จ แต่ไม่จำเป็นต้องส่งคืนเนื้อหาข้อความ
  • 400 Bad Requestหมายความว่าเซิร์ฟเวอร์ไม่สามารถดำเนินการตามคำขอได้เนื่องจากเกิดข้อผิดพลาดจากฝั่งไคลเอ็นต์
  • 401 Unauthorizedหมายความว่าเซิร์ฟเวอร์ไม่สามารถดำเนินการตามคำขอได้เนื่องจากขาดข้อมูลรับรองการตรวจสอบสิทธิ์
  • 403 Forbiddenหมายความว่าลูกค้าไม่ได้รับอนุญาตให้เข้าถึงทรัพยากร
  • 404 Not Foundหมายถึงเซิร์ฟเวอร์ไม่พบทรัพยากร (อาจเป็นเพราะถูกลบไปแล้ว)
  • 500 Internal Server Errorหมายถึงเซิร์ฟเวอร์พบข้อผิดพลาดที่ไม่คาดคิด

อะไรทำให้ API สงบ

เพื่อให้ API ได้รับการพิจารณาว่าเป็น RESTful ควรปฏิบัติตามแนวทางปฏิบัติที่ดีที่สุดบางประการ หรือที่เรียกว่าข้อจำกัดทางสถาปัตยกรรม

ข้อจำกัด 6 ประการของสถาปัตยกรรม REST คือ:

  • การแยกไคลเอ็นต์และเซิร์ฟเวอร์
  • อินเทอร์เฟซที่เหมือนกัน
  • ไร้สัญชาติ
  • ระบบเลเยอร์
  • แคชได้
  • รหัสปฏิบัติการ

เรามาดูรายละเอียดเพิ่มเติมทีละเล็กน้อยในหัวข้อต่อไปนี้

การแยกไคลเอ็นต์-เซิร์ฟเวอร์

ในสถาปัตยกรรม REST API ไคลเอ็นต์และเซิร์ฟเวอร์ต้องทำงานแยกกัน

ไคลเอ็นต์เริ่มการสื่อสารและส่งคำขอไปยังเซิร์ฟเวอร์เท่านั้น และเซิร์ฟเวอร์จะรอคำขอจากไคลเอ็นต์และตอบสนองอย่างเหมาะสม

การแยกข้อกังวลนี้ทำให้มั่นใจได้ถึงความน่าเชื่อถือและความสามารถในการปรับขนาดของระบบ

เมื่อคุณแยกไคลเอ็นต์ออกจากเซิร์ฟเวอร์ คุณสามารถเปลี่ยนโค้ดฝั่งไคลเอ็นต์ได้โดยไม่กระทบกับเซิร์ฟเวอร์ และคุณสามารถแก้ไขทรัพยากรที่จัดเก็บไว้ในเซิร์ฟเวอร์ได้โดยไม่ส่งผลกระทบต่อไคลเอนต์

อินเตอร์เฟสแบบยูนิฟอร์ม

คำขอและการตอบสนองทั้งหมดต้องเป็นไปตามรูปแบบและโปรโตคอล - จำเป็นต้องมีภาษากลางที่เป็นมาตรฐาน

ภาษาทั่วไปนี้คือ HTTP (ย่อมาจาก HyperText Transfer Protocol)

โปรดทราบว่า HTTP ไม่ได้ออกแบบมาสำหรับ REST API – REST ใช้โปรโตคอลการสื่อสาร

ไร้สัญชาติ

REST API เป็นแบบไร้สถานะ หมายความว่า API จะปฏิบัติต่อคำขอแต่ละรายการจากไคลเอนต์โดยอิสระ และคำขอนั้นจำเป็นต้องมีข้อมูลที่จำเป็นทั้งหมด

เซิร์ฟเวอร์ตีความคำขอแต่ละรายการว่าเป็นรายการใหม่ นั่นคือไม่จำข้อมูลในอดีตเกี่ยวกับไคลเอ็นต์ ตัวอย่างเช่น จำไม่ได้ว่าไคลเอนต์เคยร้องขอทรัพยากรเดียวกันมาก่อนหรือไม่

ข้อจำกัดนี้ช่วยเพิ่มประสิทธิภาพ ความสามารถในการปรับขนาด และความน่าเชื่อถือ

ระบบเลเยอร์

สถาปัตยกรรม RESTful มีโครงสร้างเป็นชั้น และแต่ละชั้นทำงานแยกกัน โดยคำขอและการตอบสนองจะผ่านชั้นลำดับชั้นที่แยกจากกัน

ไคลเอนต์และเซิร์ฟเวอร์ไม่รู้ว่ากำลังสื่อสารกับตัวกลางหรือไม่ อาจมีเซิร์ฟเวอร์อื่นๆ อีกหลายเซิร์ฟเวอร์ที่อยู่ระหว่างกลางซึ่งไม่ส่งผลกระทบต่อการโต้ตอบระหว่างไคลเอนต์กับเซิร์ฟเวอร์

เลเยอร์เหล่านี้สามารถเพิ่มความปลอดภัยและจัดการและกระจายทราฟฟิก ซึ่งช่วยเพิ่มความปลอดภัยและความสามารถในการปรับขนาดของระบบ

แคชได้

REST API ได้รับการออกแบบโดยคำนึงถึงการแคช การตอบสนองจากเซิร์ฟเวอร์ควรระบุว่าทรัพยากรสามารถแคชได้หรือไม่

หากทรัพยากรสามารถแคชได้ ไคลเอ็นต์สามารถจัดเก็บและนำข้อมูลที่เข้าถึงบ่อยมาใช้ซ้ำได้ แทนที่จะร้องขอจากเซิร์ฟเวอร์ครั้งแล้วครั้งเล่า

เมื่อทรัพยากรสามารถแคชได้ จะลดเวลาในการโหลดหน้าเว็บ เนื่องจากไคลเอ็นต์ดึงข้อมูลจากที่จัดเก็บในเครื่องและทำการเรียกไปยังเซิร์ฟเวอร์น้อยลง ในทางกลับกัน เซิร์ฟเวอร์ก็มีเวลาแฝงน้อยกว่า

รหัสปฏิบัติการ

ข้อจำกัดสุดท้ายเป็นทางเลือก

แทนที่จะส่งตัวแทนของทรัพยากรคงที่ใน JSON เซิร์ฟเวอร์อาจส่งคืนรหัสคอมพิวเตอร์บางส่วนในรูปแบบของสคริปต์ที่ไคลเอนต์สามารถดำเนินการได้

ที่กล่าวว่า API นั้นสงบแม้ว่าจะไม่มีรหัสที่เรียกใช้งานได้ก็ตาม

บทสรุป

หวังว่าบทความนี้จะเป็นประโยชน์และให้ข้อมูลเชิงลึกแก่คุณว่า REST API คืออะไร ทำงานอย่างไร และอะไรที่ทำให้ API RESTful

ขอบคุณสำหรับการอ่านและมีความสุขในการเขียนโค้ด!

ที่มาhttps://www.freecodecamp.org

#api

What is GEEK

Buddha Community

REST API คืออะไร
Wilford  Pagac

Wilford Pagac

1594289280

What is REST API? An Overview | Liquid Web

What is REST?

The REST acronym is defined as a “REpresentational State Transfer” and is designed to take advantage of existing HTTP protocols when used for Web APIs. It is very flexible in that it is not tied to resources or methods and has the ability to handle different calls and data formats. Because REST API is not constrained to an XML format like SOAP, it can return multiple other formats depending on what is needed. If a service adheres to this style, it is considered a “RESTful” application. REST allows components to access and manage functions within another application.

REST was initially defined in a dissertation by Roy Fielding’s twenty years ago. He proposed these standards as an alternative to SOAP (The Simple Object Access Protocol is a simple standard for accessing objects and exchanging structured messages within a distributed computing environment). REST (or RESTful) defines the general rules used to regulate the interactions between web apps utilizing the HTTP protocol for CRUD (create, retrieve, update, delete) operations.

What is an API?

An API (or Application Programming Interface) provides a method of interaction between two systems.

What is a RESTful API?

A RESTful API (or application program interface) uses HTTP requests to GET, PUT, POST, and DELETE data following the REST standards. This allows two pieces of software to communicate with each other. In essence, REST API is a set of remote calls using standard methods to return data in a specific format.

The systems that interact in this manner can be very different. Each app may use a unique programming language, operating system, database, etc. So, how do we create a system that can easily communicate and understand other apps?? This is where the Rest API is used as an interaction system.

When using a RESTful API, we should determine in advance what resources we want to expose to the outside world. Typically, the RESTful API service is implemented, keeping the following ideas in mind:

  • Format: There should be no restrictions on the data exchange format
  • Implementation: REST is based entirely on HTTP
  • Service Definition: Because REST is very flexible, API can be modified to ensure the application understands the request/response format.
  • The RESTful API focuses on resources and how efficiently you perform operations with it using HTTP.

The features of the REST API design style state:

  • Each entity must have a unique identifier.
  • Standard methods should be used to read and modify data.
  • It should provide support for different types of resources.
  • The interactions should be stateless.

For REST to fit this model, we must adhere to the following rules:

  • Client-Server Architecture: The interface is separate from the server-side data repository. This affords flexibility and the development of components independently of each other.
  • Detachment: The client connections are not stored on the server between requests.
  • Cacheability: It must be explicitly stated whether the client can store responses.
  • Multi-level: The API should work whether it interacts directly with a server or through an additional layer, like a load balancer.

#tutorials #api #application #application programming interface #crud #http #json #programming #protocols #representational state transfer #rest #rest api #rest api graphql #rest api json #rest api xml #restful #soap #xml #yaml

An API-First Approach For Designing Restful APIs | Hacker Noon

I’ve been working with Restful APIs for some time now and one thing that I love to do is to talk about APIs.

So, today I will show you how to build an API using the API-First approach and Design First with OpenAPI Specification.

First thing first, if you don’t know what’s an API-First approach means, it would be nice you stop reading this and check the blog post that I wrote to the Farfetchs blog where I explain everything that you need to know to start an API using API-First.

Preparing the ground

Before you get your hands dirty, let’s prepare the ground and understand the use case that will be developed.

Tools

If you desire to reproduce the examples that will be shown here, you will need some of those items below.

  • NodeJS
  • OpenAPI Specification
  • Text Editor (I’ll use VSCode)
  • Command Line

Use Case

To keep easy to understand, let’s use the Todo List App, it is a very common concept beyond the software development community.

#api #rest-api #openai #api-first-development #api-design #apis #restful-apis #restful-api

Lets Cms

Lets Cms

1652251528

Opencart REST API extensions - V3.x | Rest API Integration, Affiliate

Opencart REST API extensions - V3.x | Rest API Integration : OpenCart APIs is fully integrated with the OpenCart REST API. This is interact with your OpenCart site by sending and receiving data as JSON (JavaScript Object Notation) objects. Using the OpenCart REST API you can register the customers and purchasing the products and it provides data access to the content of OpenCart users like which is publicly accessible via the REST API. This APIs also provide the E-commerce Mobile Apps.

Opencart REST API 
OCRESTAPI Module allows the customer purchasing product from the website it just like E-commerce APIs its also available mobile version APIs.

Opencart Rest APIs List 
Customer Registration GET APIs.
Customer Registration POST APIs.
Customer Login GET APIs.
Customer Login POST APIs.
Checkout Confirm GET APIs.
Checkout Confirm POST APIs.


If you want to know Opencart REST API Any information, you can contact us at -
Skype: jks0586,
Email: letscmsdev@gmail.com,
Website: www.letscms.com, www.mlmtrees.com
Call/WhatsApp/WeChat: +91–9717478599.

Download : https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=43174&filter_search=ocrest%20api
View Documentation : https://www.letscms.com/documents/api/opencart-rest-api.html
More Information : https://www.letscms.com/blog/Rest-API-Opencart
VEDIO : https://vimeo.com/682154292  

#opencart_api_for_android #Opencart_rest_admin_api #opencart_rest_api #Rest_API_Integration #oc_rest_api #rest_api_ecommerce #rest_api_mobile #rest_api_opencart #rest_api_github #rest_api_documentation #opencart_rest_admin_api #rest_api_for_opencart_mobile_app #opencart_shopping_cart_rest_api #opencart_json_api

Lets Cms

Lets Cms

1652251629

Unilevel MLM Wordpress Rest API FrontEnd | UMW Rest API Woocommerce

Unilevel MLM Wordpress Rest API FrontEnd | UMW Rest API Woocommerce Price USA, Philippines : Our API’s handle the Unilevel MLM woo-commerce end user all functionalities like customer login/register. You can request any type of information which is listed below, our API will provide you managed results for your all frontend needs, which will be useful for your applications like Mobile App etc.
Business to Customer REST API for Unilevel MLM Woo-Commerce will empower your Woo-commerce site with the most powerful Unilevel MLM Woo-Commerce REST API, you will be able to get and send data to your marketplace from other mobile apps or websites using HTTP Rest API request.
Our plugin is used JWT authentication for the authorization process.

REST API Unilevel MLM Woo-commerce plugin contains following APIs.
User Login Rest API
User Register Rest API
User Join Rest API
Get User info Rest API
Get Affiliate URL Rest API 
Get Downlines list Rest API
Get Bank Details Rest API
Save Bank Details Rest API
Get Genealogy JSON Rest API
Get Total Earning Rest API
Get Current Balance Rest API
Get Payout Details Rest API
Get Payout List Rest API
Get Commissions List Rest API
Withdrawal Request Rest API
Get Withdrawal List Rest API

If you want to know more information and any queries regarding Unilevel MLM Rest API Woocommerce WordPress Plugin, you can contact our experts through 
Skype: jks0586, 
Mail: letscmsdev@gmail.com,
Website: www.letscms.com, www.mlmtrees.com,
Call/WhatsApp/WeChat: +91-9717478599.  

more information : https://www.mlmtrees.com/product/unilevel-mlm-woocommerce-rest-api-addon

Visit Documentation : https://letscms.com/documents/umw_apis/umw-apis-addon-documentation.html

#Unilevel_MLM_WooCommerce_Rest_API's_Addon #umw_mlm_rest_api #rest_api_woocommerce_unilevel #rest_api_in_woocommerce #rest_api_woocommerce #rest_api_woocommerce_documentation #rest_api_woocommerce_php #api_rest_de_woocommerce #woocommerce_rest_api_in_android #woocommerce_rest_api_in_wordpress #Rest_API_Woocommerce_unilevel_mlm #wp_rest_api_woocommerce

Adonis  Kerluke

Adonis Kerluke

1596509565

RESTful API Design Driven Approach

In this tutorial I will show you the fundamentals of designing a RESTful API specification by applying REST principles and best practices, then you’ll be ready to try my online tutorial: How to design a REST API with API Designer?

If you already know what is meant by API in the context of RESTful web services, you can skip to the next section. If not, read on.

Level-Set on API

The abbreviation API stands for Application Programming Interface this in itself, does not help us understand what it is, however in the context of web services, it can refer to one of two things:

  1. The RESTful API specification is written using a modeling language such as Open API specification or RAML (RESTful API Modeling Language) that defines a contract for how software components can interact with a service.
  2. The implementation of a web service or microservice whose contract is designed by REST principles that describe how other services must interact with it.

In this post, I will use the first understanding of this term. Even though both are correct, the most technically relevant for this post is the first: an API is a contract for how software applications talk to each other.

Level-Set on REST

The acronym REST stands for REpresentational State Transfer. It is an architectural style used to represent the transmission of data from one application component to another. In the context of web services, we are talking about the representation of resources (i.e. data) transferred over HTTP by calling a URI that represents the data and via an HTTP method that represents the action to perform against the given data.

What Is RESTful API design?

RESTful API design is the activity of describing the behavior of a web service in terms of its data structures and the actions you allow other application components to perform on its data by the principles of REST. Those principles are covered later in this blog.

Why Design a RESTful API?

Imagine that you are an Architect (the kind the design building) and you set out to build an office block without a blueprint. You turn up on the first day with a truck full of bricks and some cement. What are the chances that you’ll be successful and build a structure that conforms to code and more importantly, doesn’t fall? It’s about zero. Without a blueprint the chance of failure is high.

The same approach applies to web service development. You need a blueprint, or more appropriately, an API specification. This is necessary to evaluate the API design and solicit feedback before even starting to build the implementation.

In addition to providing a specification for the web service’s development, an API contract serves to document its expected behavior, data types, and security requirements.

You should now be satisfied that API design is necessary for a RESTful web service, and should start to wonder how is the best approach to actually designing an API specification.

API Design Tooling

The tooling chosen by an API designer has substantial influence over the designer’s productivity. Highly productive tools such as the Anypoint API Designer from MuleSoft is perfect for designing APIs with OAS (swagger) or RAML.

#integration #api #rest #rest api #restful #api design #raml #rest api design