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.
GET— Ma’lumot olishPOST— Yangi resurs yaratishPUT— To‘liq yangilashPATCH— Qisman yangilashDELETE— O‘chirishHEAD— Faqat header so‘rashOPTIONS— Qo‘llab-quvvatlanadigan metodlarni so‘rash
- Vazifasi: Serverdan ma’lumot so‘rash
- Body: Yo‘q
- Idempotent: ✅ Ha
- Masalan:
GET /users/123ID’si 123 bo‘lgan foydalanuvchi haqida ma’lumot olib keladi.
- 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.
- 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.
- 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
- Vazifasi: Mavjud resursni o‘chirish
- Body: ❌ Yo‘q
- Idempotent: ✅ Ha
- Masalan:
DELETE /users/123ID’si 123 bo‘lgan foydalanuvchini o‘chiradi.
- GET ga o‘xshaydi, lekin faqat header keladi.
- Body qaytmaydi.
- Ko‘pincha resurs mavjudligini tekshirish yoki cache maqsadida ishlatiladi.
HEAD /users/123- Biror endpointga nisbatan qanday HTTP metodlar ruxsat etilganini ko‘rsatadi.
- Ayniqsa CORS (Cross-Origin Resource Sharing) muammolarini hal qilishda muhim.
OPTIONS /users| 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 |
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();
}- HTTP metodlari — web API asosidir
- Har bir metod ma’lumot bilan ishlashda o‘ziga xos rolga ega
- ASP.NET Core bu metodlarni
Controllerichida qulay tarzda qo‘llab-quvvatlaydi - RESTful yondashuvda metodlar resurslar ustida CRUD amallarini bajarish uchun ishlatiladi