ترويسات HTTP الشائعة
ترويسات HTTP هي أزواج مفتاح-قيمة تُرسل في بداية كل طلب واستجابة. تحمل بيانات وصفية مثل نوع المحتوى وقواعد التخزين المؤقت والمصادقة. استخدم هذه الورقة للتعرف على الترويسات الأكثر شيوعًا عند بناء أو تصحيح حركة مرور الويب.
| الترويسة | الاتجاه | الغرض |
|---|---|---|
| Host | request | Identifies the server's host and port (from the URL). |
| User-Agent | request | Reports the client software (browser, OS, bot) making the request. |
| Accept | request | Lists the response media types (MIME) the client can handle. |
| Authorization | request | Carries credentials (token, Basic auth) to authenticate the client. |
| Content-Type | request/response | States the media type of the request or response body. |
| Content-Length | request/response | Gives the size in bytes of the request or response body. |
| Cookie | request | Sends stored key/value pairs from a previous response back to the server. |
| Set-Cookie | response | Server instructs the client to store a cookie for later requests. |
| Cache-Control | request/response | Directs caching behaviour: no-cache, max-age, private, etc. |
| ETag | response | Opaque identifier for a resource version, used for revalidation. |
| If-None-Match | request | Sends an ETag; server returns 304 if the resource is unchanged. |
| Location | response | Points the client to a new URL for redirects (3xx) or created resources. |
| Access-Control-Allow-Origin | response | Permits a browser to share a response across origins (CORS). |
| X-Forwarded-For | request | Records the client IP as seen through a proxy or load balancer. |
| Referer | request | Indicates the URL of the page that linked to the requested resource. |
ملاحظات
- أسماء الترويسات غير حساسة لحالة الأحرف؛ وتُكتب بالاصطلاح بصيغة Title-Case (مثال: Content-Type).
- ترويسات الطلب تصف العميل؛ وترويسات الاستجابة تصف جواب الخادم.
- يضبط المتصفح الكثير من الترويسات (مثل Host وReferer) تلقائيًا — فلا تزيفها.
الأسئلة الشائعة
- ما الفرق بين ترويسة الطلب وترويسة الاستجابة؟
- ترويسات الطلب يرسلها العميل (المتصفح أو التطبيق) إلى الخادم؛ وترويسات الاستجابة يعيدها الخادم مع جوابه. بعضها مثل Content-Type يظهر على الجانبين.
- هل أسماء ترويسات HTTP حساسة لحالة الأحرف؟
- لا. بحسب المواصفة أسماء الترويسات غير حساسة لحالة الأحرف، فـ Content-Type وcontent-type متساويتان. صيغة Title-Case مجرد اصطلاح.
- ماذا تفعل ترويسة Host؟
- Host تخبر الخادم أي نطاق يستهدف الطلب. وهي إلزامية في HTTP/1.1 وتتيح لخادم واحد استضافة مواقع متعددة بالاسم.
- كيف يعمل التخزين المؤقت مع الترويسات؟
- يحدد Cache-Control المدة التي يمكن فيها تخزين الاستجابة، بينما تتيح ETag وIf-None-Match للعميل أن يسأل «هل تغير؟» ويتلقى 304 Not Modified خفيفًا.