0Pricing
Serverless Backend with AWS Lambda & API Gateway · บทเรียน

การแปลงคำขอและการตอบกลับ

ปรับแต่งข้อมูลคำขอและการตอบกลับของ API Gateway ด้วยเทมเพลตการแมป (VTL) เพื่อผสานรวมกับระบบแบ็กเอนด์ที่หลากหลาย

การแปลงคำขอและการตอบกลับ เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless Backend with AWS Lambda & API Gateway และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Bridging the Data Gap

Imagine your API clients speak one data language, but your backend service understands another. How do you make them communicate smoothly?

API Gateway's request and response transformations act as a universal translator, modifying payloads to ensure compatibility between different systems.

Introducing Mapping Templates

At the heart of transformations are mapping templates. These are text-based templates written using the Velocity Template Language (VTL).

You define a VTL template that tells API Gateway exactly how to restructure or modify the incoming (request) or outgoing (response) data.

Request Transformations

Request transformations modify the data sent by your client before it reaches your backend service (like a Lambda function).

This is useful for:

  • Simplifying complex client requests for a backend.
  • Adding specific headers or parameters.
  • Converting data formats (e.g., XML to JSON, or a different JSON structure).

VTL for Request Mapping

Here's a simple VTL example for a request. It takes a client's id and qty and maps them to productId and quantity for your backend.

The $input variable gives you access to the incoming request body and parameters.

#set($body = $input.json('$'))
{
  "productId": "$body.id",
  "quantity": $body.qty
}

Accessing Request Context

Besides the body, you can access other request details like path parameters, query strings, and headers using the $context variable.

This allows you to enrich your backend's input with valuable contextual information from the API call.

#set($body = $input.json('$'))
{
  "userId": "$context.authorizer.claims.sub",
  "resourcePath": "$context.resourcePath",
  "itemId": "$input.params('itemId')",
  "requestedQuantity": $body.qty
}

Response Transformations

Response transformations modify the data returned by your backend service before it's sent back to the client.

This is crucial for:

  • Standardizing API responses across different backends.
  • Masking sensitive internal details from clients.
  • Converting backend output to a client-friendly format.

VTL for Response Mapping

Similar to requests, you use VTL to transform responses. Here, a backend's result field is mapped to a more generic data field for the client.

The $input variable in a response template refers to the backend's output.

#set($body = $input.json('$'))
{
  "status": "success",
  "data": $body.result
}

Conditional Response Mapping

API Gateway allows you to define different response templates based on the backend's HTTP status code. This means you can have a specific template for success (e.g., 200 OK) and another for errors (e.g., 400 Bad Request).

This provides granular control over how different outcomes are presented to the client.

#if($input.path('$.statusCode') == 200)
{
  "message": "Operation successful."
}
#else
{
  "error": "$input.path('$.errorMessage')"
}
#end

VTL Best Practices

When writing VTL templates:

  • Keep it simple: Avoid complex logic; delegate to your backend if needed.
  • Test thoroughly: Use API Gateway's test invocation feature.
  • Use $util.urlEncode: Encode values when placing them into URLs or query strings.
  • Handle nulls: Use $!variable to output an empty string if a variable is null.

Quick Check

You've learned how API Gateway transformations can reshape data. Which of the following are key benefits of using request/response transformations?

Recap: Mastering Transformations

You've learned how API Gateway transformations, powered by VTL mapping templates, are essential for building flexible and robust APIs.

By shaping both incoming requests and outgoing responses, you can ensure seamless communication between diverse clients and backend services, improving maintainability and reducing complexity.

คำถามที่พบบ่อย

บทเรียน “การแปลงคำขอและการตอบกลับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแปลงคำขอและการตอบกลับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless Backend with AWS Lambda & API Gateway ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแปลงคำขอและการตอบกลับ”

ปรับแต่งข้อมูลคำขอและการตอบกลับของ API Gateway ด้วยเทมเพลตการแมป (VTL) เพื่อผสานรวมกับระบบแบ็กเอนด์ที่หลากหลาย คุณปฏิบัติ Serverless Backend with AWS Lambda & API Gateway ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การแปลงคำขอและการตอบกลับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Serverless Backend with AWS Lambda & API Gateway นี้ได้ไหม

ได้ บทเรียน Serverless Backend with AWS Lambda & API Gateway ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การแคชและการจำกัดอัตรา
  2. การแปลงคำขอและการตอบกลับ
  3. ชื่อโดเมนแบบกำหนดเองและการเพิ่มประสิทธิภาพที่ขอบเครือข่าย
  4. API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์
← กลับไปที่ Serverless Backend with AWS Lambda & API Gateway