SuperJane website logo
HTTPHeaders

📡 Payload Headers

🔥 เรียนรู้เกี่ยวกับ Payload Headers ใน HTTP, การทำงาน, ตัวอย่างการใช้งาน, และบทบาทใน Software Engineering

🚀 Payload Headers คืออะไร?

Payload Headers 📦 เป็นประเภทของ HTTP Headers ที่ใช้กำหนดข้อมูลเกี่ยวกับ HTTP Message Body (Payload) ที่ถูกส่งไปยังเซิร์ฟเวอร์หรือไคลเอนต์ โดย Headers เหล่านี้จะช่วยบอกว่า:

  • 📌 ข้อมูลที่ส่งเป็นประเภทอะไร (Content-Type)
  • 📌 ข้อมูลมีขนาดเท่าไร (Content-Length)
  • 📌 ข้อมูลถูกเข้ารหัสอย่างไร (Content-Encoding)
  • 📌 มีการตรวจสอบความถูกต้องของข้อมูลหรือไม่ (Content-MD5)

Payload Headers มักใช้ใน REST APIs, File Uploads, Webhooks, Streaming และ HTTP Request ที่มีข้อมูลใน Body เช่น POST และ PUT 🚀


📌 ประเภทของ Payload Headers ที่สำคัญ

🔹 Header📝 คำอธิบาย
Content-Typeกำหนดรูปแบบของข้อมูลที่ถูกส่ง เช่น application/json, multipart/form-data, image/png
Content-Lengthระบุขนาดของ Payload (หน่วยเป็นไบต์)
Content-Encodingระบุวิธีการบีบอัดข้อมูล เช่น gzip, br, deflate
Content-Dispositionใช้กำหนดไฟล์แนบ (attachment) เช่น inline หรือ attachment; filename=\"file.pdf\"
Content-MD5ใช้ตรวจสอบความถูกต้องของ Payload โดยใช้ค่า Hash MD5
Transfer-Encodingใช้ส่งข้อมูลแบบ Chunked (chunked) เมื่อไม่ทราบขนาดไฟล์ล่วงหน้า

🔥 ตัวอย่าง Payload Headers

📡 1️⃣ ตัวอย่าง Request พร้อม Payload Headers

POST /upload HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 1234
Content-Encoding: gzip

📌 Headers ที่สำคัญในตัวอย่างนี้

  • Content-Type: application/json → ไคลเอนต์ส่งข้อมูลเป็น JSON
  • Content-Length: 1234 → Payload มีขนาด 1,234 ไบต์
  • Content-Encoding: gzip → ข้อมูลถูก บีบอัด ด้วย GZIP เพื่อให้ส่งเร็วขึ้น

🖼️ 2️⃣ ตัวอย่างการอัปโหลดไฟล์ด้วย Payload Headers

POST /upload HTTP/1.1
Host: api.example.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary
Content-Disposition: form-data; name="file"; filename="image.png"
Content-Length: 524288

📌 Headers ที่สำคัญในตัวอย่างนี้

  • Content-Type: multipart/form-data → ใช้สำหรับอัปโหลดไฟล์
  • Content-Disposition: form-data; name="file"; filename="image.png" → กำหนดชื่อไฟล์ที่ส่ง
  • Content-Length: 524288 → ไฟล์มีขนาด 524 KB

⚡ Payload Headers ใน Software Engineering

🔗 ใช้กับ REST APIs

POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 200

💡 Backend เช่น Node.js หรือ Django ต้องกำหนด Headers ให้ตรงกัน เพื่อรองรับการประมวลผลข้อมูล 🚀


🏗️ ใช้ใน Microservices และ gRPC

Content-Type: application/grpc+proto
Content-Encoding: gzip

💡 ช่วยลด Bandwidth และ Latency ในการสื่อสารระหว่าง Microservices


📡 ใช้กับ WebSockets และ Streaming

HTTP/1.1 200 OK
Transfer-Encoding: chunked

💡 ช่วยให้สามารถส่งข้อมูลแบบสตรีมมิ่งโดยไม่ต้องระบุขนาดไฟล์ล่วงหน้า


🛠️ การตั้งค่า Payload Headers ใน Backend (Node.js)

const express = require("express");
const app = express();
 
app.use(express.json()); // รองรับ JSON payload
 
app.post("/data", (req, res) => {
  console.log(req.headers["content-type"]); // application/json
  console.log(req.headers["content-length"]); // แสดงขนาดข้อมูลที่รับมา
  res.set("Content-Encoding", "gzip");
  res.send("Data received ✅");
});
 
app.listen(3000, () => console.log("🚀 Server running on port 3000"));

🎯 สรุป

Payload Headers เป็น Headers ที่ใช้กำหนดลักษณะของ HTTP Message Body (Payload)
✅ ใช้กำหนด ประเภทข้อมูล, การบีบอัด, ขนาด และการตรวจสอบความถูกต้องของ Payload
✅ ใช้กันอย่างแพร่หลายใน REST APIs, Microservices, File Uploads และ Streaming
✅ สำคัญมากใน Software Engineering โดยช่วยเพิ่มประสิทธิภาพ, ความปลอดภัย และรองรับการใช้งานที่ซับซ้อน

💡 นักพัฒนา Backend และ Full Stack ควรเข้าใจ Payload Headers เพื่อนำไปใช้พัฒนา API และเพิ่มประสิทธิภาพของระบบ 🚀