HTTP 200 OK: if you want send some additional data in the Response body. Usually used for GET request
HTTP 201 Created The request has been fulfillled, a new entity has been created. Response might contain location header pointing to the entity. So insted of returning actual entity usually a 201 response includes a pointer to the location of the entity. Usualyy used for POST request and sometime for PUT request.
HTTP 202 Accepted the request has been accepted and is pending processing. So if I send the server a large cunk of entities to process, and the process might take time. No notification is given when processing is complete. Usually used for POST an PUT request. Since these requests are a request for processing data and the client usually does not have to wait until these actions are complete
HTTP 404 NotFound the source we were looking for is Not Found. We are going to use it when a specific entity was looked for, not with query parameter. When we are looking for a specific entity using the ID Paremeter and this entity is not found we should response 404. in contrast, when a request is made with query parameters and nothing is returned. we can return 200. Because There is no entity that match the parameters and this is accetable.
GET /users 200 [John, Peter]
GET /users/john 200 John
GET /unknown-url-egaer 404 Not Found
GET /users/kyle 404 User Not found
GET /users?name=kyle` 200 []
DELETE /users/john 204 No Content
Get metodları için;
Eğer GetAll sorgusu yapıyorsak resource olsada olmasada geriye 200-Ok döner
api/v1/Magazines
[HttpGet]
public IActionResult GetMagazines()
{
return Ok(DumyData.GetMagazines());
}
Eğer ID parameter ile sorgu yapıyorsak ID Parametresine karşılık bir resource yoksa NotFound döner. ID Parametresine karşılık resource varsa 200-OK döner.
api/v1/Magazines/1
[HttpGet("{id}")]
public IActionResult GetMagazineById(int id)
{
var magazine = DumyData.GetMagazineById(id);
if (magazine == null)
{
return NotFound();
}
return Ok(magazine);
}
Eğer Query string ile sorgu yapıyorsak filtremize karşılık bir resource olsada olmada geriye 200-Ok döneriz.
GET api/v1/Magazines/2/articles/filter?page=100
[HttpGet("{magazineId}/articles/filter")]
public IActionResult GetAllArticlesByMagazineIdAndFilter(int magazineId, [FromQuery] int page)
{
var magazine = DumyData.GetMagazineById(magazineId);
if (magazine is null)
{
return NotFound();
}
var data = DumyData.GetArticleByMagazineId(magazineId).FindAll(x => x.Page == page);
return Ok(data);
}
Post metodları için;
Post işlemi sonucu Status Code olarak 201 created dönmemiz gerekiyor. Bununla birlikte ilgili resource bilgilerini dönmemiz gerekiyor ve son olarak Response Header Location Field ile post edilen resource için URI bilgisini dönmemiz gerekiyor.
// api/v1/Magazines/2/articles [HttpPost("{magazineId}/articles")] public IActionResult AddArticle(int magazineId, [FromBody]ArticleCreateDTO articleDto) { var magazine = DumyData.GetMagazineById(magazineId); if(magazine is null) { return NoContent(); } var article = DumyData.AddArticle(magazineId, articleDto); return CreatedAtAction("GetArticleByMagazineIdAndArticleId", new { magazineId = magazineId, id = article.Id }, article); } // api/v1/Magazines/2/articles/5 [HttpGet("{magazineId}/articles/{id}")] public IActionResult GetArticleByMagazineIdAndArticleId( int magazineId, int id) { var article = DumyData.GetArticleById(id); if (article is null) { return NotFound(); } return Ok(article); }
Put metodları için;
Put Http metodu resource'a yeni bir item eklemek veya var olan resource'u update etmek için kullanılır. Eğer yeni bir item eklenirse Post methodu gibi davranır. ve 201 created dönmeliyiz. Eğer var olan bir itemi günceliyorsa 204 No Content status kodu ile dönüş yapmalıdır.
// api/v1/Magazines/1/articles/8 [HttpPut("{magazineId}/articles/{id}")] public IActionResult UpdateArticle(int magazineId, int id, [FromBody]ArticleUpdateDTO articleDto) { var magazine = DumyData.GetMagazineById(magazineId); if(magazine is null) { return NotFound(); } var article = DumyData.GetArticleById(magazineId, id); if(article is null) { article = DumyData.AddArticle(magazineId, new ArticleCreateDTO() { Author = articleDto.Author, Name = articleDto.Name, Page = articleDto.Page}); return CreatedAtAction("GetArticleByMagazineIdAndArticleId", new { magazineId = magazineId, id = article.Id }, article);
} DumyData.UpdateArticle(magazineId, id, articleDto); return NoContent(); }
Delete metodları için;
Eğer silinmek istenen resource yok ise 404 Not Found döneriz.Silme işlemi başarılı gerçekleşirse 204 No Content dönmeliyiz.
.
api/v1/Magazines/1/articles/2
[HttpDelete("{magazineId}/articles/{id}")]
public IActionResult DeleteArticle(int magazineId, int id)
{
var magazine = DumyData.GetMagazineById(magazineId);
if (magazine is null)
{
return NotFound();
}
var item = DumyData.GetArticleById(magazineId, id);
if (item is null)
{
return NotFound();
}
DumyData.DeleteArticle(magazineId, id);
return NoContent();
}
.
Hiç yorum yok:
Yorum Gönder