Automatically translated.View original post

Rest API...How does it work?

# Web Site Writing Basics

# nodejs # RestApi # Drug sign with lemon8 # Trending

🌐 what is the REST API? The REST API, short for REpresentational State Transfer Application Programming Interface in a simple sense, is a set of architectural rules and principles for creating web services that allow two different software systems to communicate, exchange information with each other across the Internet in a standard way 🗝️. The core of RESTREST focuses on communication based on "resources," where each resource is identified by a unique Uniform Resource Locator (Resource) URL: is any information or anything the system needs to manage, such as users, users, products, words. Orders: When a Client (such as a web browser or a mobile application) requests a resource, the data is sent back in a form called "Representation," most often JSON (JavaScript Object Notation) or 🛠️ REST APIREST API. The principle of REST APIREST API uses the HTTP Protocol (Hypertext Transfer Protocol) as the basis for communication. It follows the Client-Server format and uses the "Verb" or HTTP Method to specify the action you want to do per that "resource." 1. The request (The Request) Method (the requester) sends the request for HTTP (the main resource) and the https://api.example.com/products/123HTTP the server (the server). HTTP Methods (CRUD Operations) Major HTTP Methods are used to perform CRUD (Create, Read, Update, Delete) on Resources: HTTP Execution Method (CRUD) Semantics Sample URI (Assume User Entry) GETRead Retrieve Resources from ServerGET / User (Retrieve All Users) or GET / User / 1 (Retrieve User ID 1) POSTCreate New Resource POST / User (Create New User) PUTUpdate Existing Resources Total PUT / User / 1 (Replace All User ID 1 Data) DELETEDelete Specified Resources DETE / User ID (Delete User 1) PATCHUpdate Available Resources Partial PATCH / user / 1 (just edit the name of user ID 1) 3. The Response Server will process the request and send back the HTTP reply to Client. It includes: Status Code: Status Code Numbers that tell you the result of the request. 200 OK: Successful 201 Created: Successfully Rebuilt Resources (Usually Used on POST) 400 Bad Request: Invalid Request404 Not Found: No Resources Found 500 Internal Server Error: ServerHeaders Error: Additional Information on ResponseBody (Payload): Information of Client's requested resources (such as JSON-format user data) 💡 Important limitations (Statelessness) The key principle of REST is Stateless. (Stateless) means: Server must not store any information about Client's previous requests. Each request must contain all the information needed to process this principle, making the API flexible and scalable. Because Server can handle requests from any Client without remembering what Client has done.

2025/12/13 Edited to

... Read moreในยุคปัจจุบันที่เทคโนโลยีขับเคลื่อนการเชื่อมต่อข้อมูลกันอย่างรวดเร็ว REST API จึงกลายเป็นเครื่องมือสำคัญสำหรับนักพัฒนาเว็บและแอปพลิเคชันมือถือ โดย REST API เป็นมาตรฐานที่อำนวยความสะดวกในการสื่อสารระหว่างโปรแกรมสองระบบผ่านทาง HTTP Protocol ซึ่งเป็นโพรโทคอลที่ใช้กันอย่างกว้างขวางบนอินเทอร์เน็ต หนึ่งในจุดเด่นของ REST API คือการยึดหลักของการจัดการทรัพยากร (Resources) ที่ถูกระบุด้วย URL เฉพาะเจาะจง ทำให้การร้องขอข้อมูลหรือส่งข้อมูลใหม่เป็นไปอย่างชัดเจนและเป็นระบบ HTTP Methods อย่าง GET, POST, PUT, DELETE, และ PATCH จะทำหน้าที่กำหนดให้ Server รู้ว่าควรทำอะไรกับทรัพยากรนั้น เช่น GET สำหรับดึงข้อมูล, POST สำหรับสร้างข้อมูลใหม่, PUT และ PATCH สำหรับการแก้ไข และ DELETE สำหรับการลบข้อมูล นอกจากนี้ REST API ยังมีข้อกำหนดสำคัญคือ Statelessness ซึ่ง Server จะไม่จดจำสถานะของ Client แต่ละตัวหลังจบคำร้อง ทำให้การจัดการคำขอในระบบมีความยืดหยุ่นและรองรับการขยายระบบได้ดี เหมาะกับบริการออนไลน์ที่มีผู้ใช้จำนวนมากและกระจายตัวอยู่ทั่วโลก ส่วนการตอบกลับจาก Server จะประกอบด้วย Status Code ที่ช่วยให้ Client รู้สถานะของคำขอ เช่น 200 OK หมายถึงคำขอสำเร็จ, 201 Created หมายถึงข้อมูลถูกสร้างขึ้นเรียบร้อย, หรือ 404 Not Found หากไม่พบข้อมูลที่ร้องขอ พร้อมทั้งมีส่วนของ Body ที่ส่งข้อมูลในรูปแบบที่เข้าใจง่ายอย่าง JSON หรือ XML เพื่อให้โปรแกรมประมวลผลต่อไปได้อย่างมีประสิทธิภาพ ในแง่ของความปลอดภัย REST API สามารถใช้ระบบ Authentication ผ่าน Headers เพื่อยืนยันตัวตนของ Client ก่อนอนุญาตให้เข้าถึงทรัพยากรต่าง ๆ ได้ ทำให้นักพัฒนาสามารถออกแบบระบบที่ทั้งง่ายต่อการใช้งานและมีมาตรฐานสูง ด้วยเหตุนี้ REST API จึงเป็นเทคโนโลยีที่ได้รับการยอมรับและนิยมใช้อย่างกว้างขวางสำหรับการพัฒนาระบบเว็บและแอปพลิเคชันยุคใหม่ที่ต้องการความรวดเร็วในการสื่อสารข้อมูลและความสอดคล้องในการจัดการทรัพยากรต่าง ๆ ผ่านระบบเครือข่ายอินเทอร์เน็ต