http คืออะไร?

รวมถึงการ Request, Response มีหน้าตายังไง

Tanabodin Kamol
Nov 4 · 3 min read

http คืออะไร ?

http ย่อมาจาก Hypertext Transfer Protocol คือโพรโทคอลสื่อสารผ่าน internet ใช้ในการรับและส่งข้อมูล ในการแลกเปลี่ยนข้อมูลระหว่าง Client และ Server โดยการส่งจาก Client ไป Server จะเรียกว่า http request ส่วนข้อมูลที่ Server ตอบกลับมาที่ Client จะเรียกว่า http Response


http — Request

รูปแบบของ request จาก Client ไป Server จะเป็นดังต่อไปนี้ อ้างอิงจากลิ้งนี้

เมื่อเอาโครงสร้างของ Request message ไปเทียบกับข้อมูลตอนที่เราใช้งานจริง จะแบ่งแต่ละส่วนได้แบบภาพข้างล่างนี้

CRLF หมายถึง Carriage Return(\r) Line Feed(\n) หรือการขึ้นบรรทัดใหม่นั่นเอง

Request-Line

Request-Line จะเริ่มด้วย token method ตามด้วย Request-URI และ version ของ protocol จบด้วย CRLF ทุกส่วนจะถูกคั่นด้วยช่องว่าง (อักขระ SP)

เช่น GET /hello.html HTTP/1.1

Request Method

http request จะกำหนด action ที่จะกระทำต่อข้อมูล method จะมีลักษณะเป็นตัวพิมพ์ใหญ่ทั้งหมดเสมอ

โดย method ที่สำคัญมี 4 ตัวคือ GET, POST, PUT, DELETE

  • GET — เป็นการเรียกรับข้อมูลจาก URI ที่กำหนด method GET ควรใช้ในการดึงข้อมูลเท่านั้นและต้องไม่มีผลกระทบใด ๆ กับข้อมูล
  • POST — ใช้สำหรับการสร้างข้อมูลใหม่โดยส่งข้อมูลผ่าน body
  • PUT — ใช้สำหรับแทนที่ข้อมูลที่มีทั้งหมดด้วยข้อมูลใหม่ที่ส่งขึ้นไป
  • DELETE — ใช้สำหรับลบข้อมูลที่มีอยู่ ของเป้าหมายที่กำหนดโดย URI

Request Header Fields

header จะให้ ผู้ใช้ สามารถส่งผ่านข้อมูลเพิ่มเติมเกี่ยวกับ Request ของผู้ใช้ เช่น เป็นภาษาอะไร, ชนิดข้อมูลเป็นแบบไหน และนี่คือรายชื่อของ header ที่สำคัญบางอย่างที่สามารถเลือกใช้ได้

  • Accept-Charset
  • Accept-Encoding
  • Accept-Language
  • Connection
  • Content-Length
  • Content-Type
  • Cookie
  • Host
  • User-Agent

Message-body

เป็นส่วนสุดท้ายของ Request message จะเป็นส่วนที่ใช้สำหรับส่งข้อมูลให้กับ server อาจจะมีหรือไม่มีการส่งก็ได้ขึ้นอยู่กับบริการที่เราเรียกด้วย

ตัวอย่าง Request message


http — Response

การ Response จะเกิดขึ้นหลังจากส่ง Request ไปที่ server แล้ว server ก็จะมีการตอบรับกลับมาซึ่งเราจะเรียกข้อความที่ตอบกลับมาว่า http-Response message โดย Response message จะประกอบไปด้วย 3 ส่วนหลักคือ Status-Line, Response Headers และ Message-body อ้างอิงจากลิ้งนี้

Status-Line

Status-Line จะเริ่มด้วย version ของ protocol ตามด้วย status-code และ Reason-Phase จบด้วย CRLF ทุกส่วนจะถูกคั่นด้วยช่องว่าง(อักขระ SP)

Status-code

http status-code จะแบ่งออกเป็นหมวดหมู่ตามเลขที่อยู่ตัวหน้าสุด และที่ยกมาไว้จะเป็น Response ที่มักจะเจอ โดย Response อื่น ๆ สามารถไปอ่านต่อได้ใน Status Code Definitions เพื่อดูว่า status-code มีอะไรบ้างและความหมายอย่างไร

2xx (สำเร็จ) หมายความว่าการ request นั้นได้รับแล้วและกระทำตาม method สำเร็จโดย Server

  • 200 Ok เป็นมาตรฐานของ HTTP Response นั้น Success สำหรับ GET, PUT หรือ POST
  • 201 Create เป็น Response สำหรับข้อมูลใหม่ได้ถูกสร้างขึ้น ใช้สำหรับ POST
  • 204 No Content เป็น Response สำหรับ request ที่ดำเนินการ Success แต่ไม่ได้ return ข้อมูลกลับ

3xx (Redirection)

  • 304 Not Modified เป็น status code ที่บอกว่า client ได้รับการ Response แล้วอยู่ใน cache และไม่จำเป็นจะต้องส่งผ่านข้อมูลเดิมอีกครั้ง

4xx (Client error) โดย status ในกลุ่มนี้จะบอก client ว่า request ที่เข้ามา error

  • 400 Bad Request บอกว่า request ที่ส่งมาโดย client นั้นไม่ถูกดำเนินการ และ Server ไม่เข้าใจว่า request เกี่ยวกับอะไร
  • 401 Unauthorized บอกว่า client ไม่ได้รับอนุญาตในการเข้าถึงข้อมูลและควรจะส่ง credential มาพร้อม request
  • 403 Forbidden บ่งบอกว่า request นั้นถูกต้องและ client ได้รับการอนุญาต แต่ Client ไม่ได้รับการอนุญาตให้เข้าถึงข้อมูลด้วยเหตุผลบางประการ
  • 404 Not Found บ่งบอกว่า resource ที่ request มานั้น ไม่ว่างใช้งานตอนนี้
  • 405 Gone บ่งบอกว่าข้อมูลที่ต้องการนั้นไม่มีอยู่แล้ว หรืออาจจะย้ายไปที่อื่น

5xx (Server error)

  • 500 Internal Server Error บอกว่าการ request นั้นถูกต้อง แต่ server มีความสับสนและจะบริการด้วยเงื่อนไขที่คาดการไม่ได้
  • 503 Service Unavailable บอกว่า server ใช้การไม่ได้ หรือไม่ว่างที่จะรับและดำเนินการ request โดยส่วนใหญ่แล้ว server อยู่ในช่วงบำรุงรักษา

REFERENCE

  1. https://www.tutorialspoint.com/http/http_requests.htm
  2. https://www.tutorialspoint.com/http/http_responses.htm
  3. https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1

iCreativeSystems

Developing and manufacturing autonomous and automation system, equipped with the company’s proprietary hardware and software systems.

Tanabodin Kamol

Written by

I always self-study about electronic devices and computer programming, So, I will share what I have learned for all of you! Sometime It’s code for Python

iCreativeSystems

Developing and manufacturing autonomous and automation system, equipped with the company’s proprietary hardware and software systems.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade