0Pricing
Serverless Backend with AWS Lambda & API Gateway · درس

تحويلات الطلبات والاستجابات

خصّص حمولات طلبات API Gateway واستجاباته باستخدام قوالب التعيين (VTL) للتكامل مع أنظمة backend متنوعة

تحويلات الطلبات والاستجابات درس مجاني في Serverless Backend with AWS Lambda & API Gateway على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.

الأسئلة الشائعة

هل درس «تحويلات الطلبات والاستجابات» مجاني؟

نعم — نص درس «تحويلات الطلبات والاستجابات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Serverless Backend with AWS Lambda & API Gateway، انتقل إلى CoddyKit PRO. تتضمن دورة Serverless Backend with AWS Lambda & API Gateway 4 دروس في المجموع.

ماذا ستتعلم في «تحويلات الطلبات والاستجابات»؟

خصّص حمولات طلبات API Gateway واستجاباته باستخدام قوالب التعيين (VTL) للتكامل مع أنظمة backend متنوعة تتمرن على Serverless Backend with AWS Lambda & API Gateway مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التخزين المؤقت وتقييد المعدل
  2. تحويلات الطلبات والاستجابات
  3. أسماء النطاقات المخصّصة وتحسين الحافة
  4. واجهات WebSocket API للاتصال الفوري
← العودة إلى Serverless Backend with AWS Lambda & API Gateway