Skip to content

Instantly share code, notes, and snippets.

@sirojiddin0198
Last active July 24, 2025 05:40
Show Gist options
  • Select an option

  • Save sirojiddin0198/a0bb2ffa69997f03e9e57af18deaab44 to your computer and use it in GitHub Desktop.

Select an option

Save sirojiddin0198/a0bb2ffa69997f03e9e57af18deaab44 to your computer and use it in GitHub Desktop.
HTTP Verbs

🌐 HTTP metodlari (verbs) va ularning RESTful API’dagi o‘rni

HTTP metodlari (yoki HTTP verbs) — bu HTTP protokoli orqali serverga yuboriladigan so‘rovning qanday harakat bajarilishini bildiruvchi buyruqlardir.
RESTful arxitektura asosidagi web ilovalarda ular asosiy ahamiyatga ega, chunki ular orqali CRUD (Create, Read, Update, Delete) amallar bajariladi.


🧩 Asosiy HTTP metodlari turlari :

  • GET — Ma’lumot olish
  • POST — Yangi resurs yaratish
  • PUT — To‘liq yangilash
  • PATCH — Qisman yangilash
  • DELETE — O‘chirish
  • HEAD — Faqat header so‘rash
  • OPTIONS — Qo‘llab-quvvatlanadigan metodlarni so‘rash

🔹 1. GET — Ma’lumot olish

  • Vazifasi: Serverdan ma’lumot so‘rash
  • Body: Yo‘q
  • Idempotent: ✅ Ha
  • Masalan:
GET /users/123

ID’si 123 bo‘lgan foydalanuvchi haqida ma’lumot olib keladi.


🔹 2. POST — Yangi resurs yaratish

  • Vazifasi: Serverga yangi ma’lumot (obyekt) yuborish
  • Body: ✅ Ha (odatda JSON)
  • Idempotent: ❌ Yo‘q
  • Masalan:
POST /users
{
  "name": "Ali",
  "email": "ali@example.com"
}

Yangi foydalanuvchi yaratadi.


🔹 3. PUT — To‘liq yangilash

  • Vazifasi: Mavjud resursni butunlay yangilash
  • Body: ✅ Ha
  • Idempotent: ✅ Ha
  • Masalan:
PUT /users/123
{
  "name": "Vali",
  "email": "vali@example.com"
}

ID’si 123 bo‘lgan foydalanuvchini to‘liq yangilaydi.


🔹 4. PATCH — Qisman yangilash

  • Vazifasi: Ma’lumotning faqat bir qismini yangilash
  • Body: ✅ Ha
  • Idempotent: ➖ Nisbiy (kerakli ehtiyot bilan ishlatish kerak)
  • Masalan:
PATCH /users/123
{
  "email": "new@example.com"
}

Faqat email qiymatini yangilaydi.


🔹 5. DELETE — O‘chirish

  • Vazifasi: Mavjud resursni o‘chirish
  • Body: ❌ Yo‘q
  • Idempotent: ✅ Ha
  • Masalan:
DELETE /users/123

ID’si 123 bo‘lgan foydalanuvchini o‘chiradi.


🔹 6. HEAD — Faqat header so‘rash

  • GET ga o‘xshaydi, lekin faqat header keladi.
  • Body qaytmaydi.
  • Ko‘pincha resurs mavjudligini tekshirish yoki cache maqsadida ishlatiladi.
HEAD /users/123

🔹 7. OPTIONS — Qo‘llab-quvvatlanadigan metodlarni so‘rash

  • Biror endpointga nisbatan qanday HTTP metodlar ruxsat etilganini ko‘rsatadi.
  • Ayniqsa CORS (Cross-Origin Resource Sharing) muammolarini hal qilishda muhim.
OPTIONS /users

📊 Jadval ko‘rinishida taqqoslash

Verb Maqsad Body kerakmi? Idempotent Misol
GET Ma’lumot olish Yo‘q GET /users/1
POST Yangi ma’lumot POST /users
PUT To‘liq yangilash PUT /users/1
PATCH Qisman yangilash PATCH /users/1
DELETE O‘chirish Yo‘q DELETE /users/1
HEAD Faqat header olish Yo‘q HEAD /users/1
OPTIONS Ruxsat metodlarni olish Yo‘q OPTIONS /users

🚀 HTTP metodlari va ASP.NET Core Web API

ASP.NET Core’da har bir HTTP metodiga [HttpGet], [HttpPost], [HttpPut] va h.k. atributlar mos keladi. Misol:

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    [HttpGet("{id}")]
    public IActionResult Get(int id) => Ok(...);

    [HttpPost]
    public IActionResult Create([FromBody] UserDto dto) => Created(...);

    [HttpPut("{id}")]
    public IActionResult Update(int id, [FromBody] UserDto dto) => Ok(...);

    [HttpPatch("{id}")]
    public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument<UserDto> patch) => Ok(...);

    [HttpDelete("{id}")]
    public IActionResult Delete(int id) => NoContent();
}

📚 Xulosa

  • HTTP metodlari — web API asosidir
  • Har bir metod ma’lumot bilan ishlashda o‘ziga xos rolga ega
  • ASP.NET Core bu metodlarni Controller ichida qulay tarzda qo‘llab-quvvatlaydi
  • RESTful yondashuvda metodlar resurslar ustida CRUD amallarini bajarish uchun ishlatiladi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment