![[AWS Technical Support Note] อยากรู้วิธีการเปลี่ยน response เมื่อมี request ไปยัง API method ที่ไม่มีอยู่ใน API Gateway](https://devio2024-media.developers.io/image/upload/v1757672716/user-gen-eyecatch/mg9eqssoqtynwt6qy01p.png)
[AWS Technical Support Note] อยากรู้วิธีการเปลี่ยน response เมื่อมี request ไปยัง API method ที่ไม่มีอยู่ใน API Gateway
ปัญหาที่เกิดขึ้น
API Gateway สามารถตรวจสอบและส่งข้อความ error เฉพาะเจาะจงเมื่อมีการร้องขอไปยัง method ที่ไม่ได้สร้างไว้หรือไม่?
วิธีแก้ปัญหา
ตั้งค่าที่ gateway responses ของ API
วิธีนี้อ้างอิงจากข้อมูลในบทความอ้างอิง[1]เกี่ยวกับวิธีแก้ปัญหา "Missing Authentication Token ว่าให้ลองเช็คดูว่ามีการปรับแต่งที่ gateway responses อยู่หรือไม่
Missing authentication token
Gateway response สำหรับข้อผิดพลาด missing authentication token รวมถึงกรณีที่ไคลเอนต์พยายามเรียกใช้ API method หรือ resource ที่ไม่รองรับ หากไม่ได้ระบุประเภทของ response การตอบกลับนี้จะใช้ค่าเริ่มต้นเป็นประเภท DEFAULT_4XX
สามารถดูวิธีการปรับแต่งการตอบกลับ error ของ "Missing Authentication Token" ได้ที่บทความอ้างอิง [2][3] ด้านล่าง
บทความอ้างอิง
[1] How do I troubleshoot API Gateway REST API endpoint 403 "Missing Authentication Token" errors? (English)
API Gateway REST API endpoints return Missing Authentication Token errors for the following reasons:
The API request is made to an operation or resource that doesn't exist.
The API request isn't signed when the AWS Identity and Access Management (IAM) authentication is turned on for the API operation.
(แปลไทย)
API Gateway REST API endpoints ส่งคืน error Missing Authentication Token ด้วยเหตุผลดังต่อไปนี้:
・การร้องขอ API ถูกส่งไปยัง operation หรือ resource ที่ไม่มีอยู่
・การร้องขอ API ไม่ได้ถูกลงนามเมื่อการตรวจสอบสิทธิ์ AWS Identity and Access Management (IAM) ถูกเปิดใช้งานสำหรับ API operation
[2] Gateway responses for REST APIs in API Gateway (English)
Setting up gateway responses to customize error responses
If API Gateway fails to process an incoming request, it returns to the client an error response without forwarding the request to the integration backend. By default, the error response contains a short descriptive error message. For example, if you attempt to call an operation on an undefined API resource, you receive an error response with the { "message": "Missing Authentication Token" } message. If you are new to API Gateway, you may find it difficult to understand what actually went wrong.
(แปลไทย)
การตั้งค่า gateway responses เพื่อปรับแต่งการตอบกลับข้อผิดพลาด
หาก API Gateway ไม่สามารถประมวลผลคำขอที่เข้ามาได้ มันจะส่งคืนการตอบกลับข้อผิดพลาดไปยังไคลเอนต์โดยไม่ส่งต่อคำขอไปยัง integration backend โดยค่าเริ่มต้น การตอบกลับข้อผิดพลาดจะมีข้อความอธิบายข้อผิดพลาดสั้นๆ ตัวอย่างเช่น หากคุณพยายามเรียกใช้ operation บน API resource ที่ไม่ได้กำหนดไว้ คุณจะได้รับการตอบกลับข้อผิดพลาดพร้อมข้อความ { "message": "Missing Authentication Token" } หากคุณเป็นมือใหม่กับ API Gateway คุณอาจพบว่ามันยากที่จะเข้าใจว่าเกิดอะไรขึ้นจริงๆ
สำหรับการตอบกลับข้อผิดพลาดบางประเภท API Gateway อนุญาตให้นักพัฒนา API ปรับแต่งเพื่อส่งคืนการตอบกลับในรูปแบบที่แตกต่างกัน สำหรับตัวอย่าง Missing Authentication Token คุณสามารถเพิ่มคำแนะนำไปยัง response payload เดิมพร้อมสาเหตุที่เป็นไปได้ เช่นในตัวอย่างนี้: {"message":"Missing Authentication Token", "hint":"The HTTP method or resources may not be supported."}
[3] Set up a gateway response for a REST API using the API Gateway console (English)
บทความต้นฉบับ
API Gateway で 存在しない API メソッドへリクエストした際のレスポンスを変更する方法を教えてください (Japanese)