Skip to content

Commit 00e8a87

Browse files
committed
Use HTTP Status Codes
1 parent 30e6983 commit 00e8a87

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

Contracts/interfaces/IBookService.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ paths:
4343
responses:
4444
200:
4545
description: OK
46+
201:
47+
description: "Book created"
48+
409:
49+
description: "Book exist"
4650
default:
4751
description: failed
4852
schema:
@@ -64,6 +68,10 @@ paths:
6468
responses:
6569
200:
6670
description: OK
71+
404:
72+
description: "Book not found"
73+
405:
74+
description: "Validation exception"
6775
default:
6876
description: failed
6977
schema:
@@ -83,6 +91,8 @@ paths:
8391
tags:
8492
- IBookService
8593
responses:
94+
404:
95+
description: "Book not found"
8696
200:
8797
description: OK
8898
schema:

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,62 @@ In most cases the best thing is to create yet a WCF service using the same contr
118118
- GET <http://localhost:15563/RestService1.svc/Book/1> in Postman
119119
- => Response: `{"Id": 1, "Name": "The incredible stamp"}`
120120

121+
## Use HTTP Status Codes
122+
123+
18. Add HTTP Status Codes to your service
124+
- In your service `RestService1.svc.cs` - method `AddBook()` add content
125+
```CSharp
126+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created; // 201
127+
if (book.Name == "The incredible stamp") { // Book exist
128+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Conflict; // 409
129+
}
130+
```
131+
- In method `UpdateBook()` add content
132+
```CSharp
133+
if (book.Id == 2) { // Book does not exist - 404
134+
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Resource not found");
135+
} else if (book.Name == "") { // Invalid request
136+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.MethodNotAllowed; // 405
137+
}
138+
```
139+
- In method `GetBookById()` add content
140+
```CSharp
141+
if (id == "2") { // Book does not exist - 404
142+
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Resource not found");
143+
return null;
144+
} else {
145+
return new Book() { Id = 1, Name = "The incredible stamp" };
146+
}
147+
```
148+
- Follow the guidance for HTTP Status Codes in <https://developers.redhat.com/blog/2017/01/19/applying-api-best-practices-in-fuse/>
149+
- => Test the change using Postman
150+
19. Update your yaml with the Status Codes
151+
- In `IBookService.yaml`
152+
```yaml
153+
put:
154+
responses:
155+
201:
156+
description: "Book created"
157+
409:
158+
description: "Book exist"
159+
post:
160+
responses:
161+
404:
162+
description: "Book not found"
163+
405:
164+
description: "Validation exception"
165+
get:
166+
responses:
167+
404:
168+
description: "Book not found"
169+
```
170+
121171
## Refs
122172
- Postman: <https://www.getpostman.com/> or <https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en>
123173
- Swagger4WCF: <https://www.codeproject.com/Tips/1190441/How-to-generate-basic-swagger-yaml-description-for>
124174
- NuGet Swagger4WCF: <https://www.nuget.org/packages/Swagger4WCF>
125175
- Unity.WCF: <https://www.devtrends.co.uk/blog/introducing-unity.wcf-providing-easy-ioc-integration-for-your-wcf-services>
176+
- Set StatusCode: <https://codeblitz.wordpress.com/2009/04/27/how-to-host-and-consume-wcf-restful-services/>
177+
- API Best Practices: <https://developers.redhat.com/blog/2017/01/19/applying-api-best-practices-in-fuse/#>
126178
127179
The End

WebApplicationWcfRest1/README.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,55 @@
9898
- GET http://localhost:15563/RestService1.svc/Book/1 in Postman
9999
=> Response: {"Id": 1, "Name": "The incredible stamp"}
100100

101+
-------- TFS Changeset 8 - Use HTTP Status Codes ---------------------
102+
18. Add HTTP Status Codes to your service
103+
- In your service RestService1.svc.cs
104+
-- In method AddBook() add content
105+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created; // 201
106+
if (book.Name == "The incredible stamp") { // Book exist
107+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Conflict; // 409
108+
}
109+
-- In method UpdateBook() add content
110+
if (book.Id == 2) { // Book does not exist - 404
111+
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Resource not found");
112+
} else if (book.Name == "") { // Invalid request
113+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.MethodNotAllowed; // 405
114+
}
115+
-- In method GetBookById() add content
116+
if (id == "2") { // Book does not exist - 404
117+
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Resource not found");
118+
return null;
119+
} else {
120+
return new Book() { Id = 1, Name = "The incredible stamp" };
121+
}
122+
- Follow the guidance for HTTP Status Codes in https://developers.redhat.com/blog/2017/01/19/applying-api-best-practices-in-fuse/
123+
=> Test the change using Postman
124+
19. Update your yaml with the Status Codes
125+
- In IBookService.yaml
126+
--
127+
put:
128+
responses:
129+
201:
130+
description: "Book created"
131+
409:
132+
description: "Book exist"
133+
post:
134+
responses:
135+
404:
136+
description: "Book not found"
137+
405:
138+
description: "Validation exception"
139+
get:
140+
responses:
141+
404:
142+
description: "Book not found"
143+
101144
Ref:
145+
- Postman: https://www.getpostman.com/ or https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en
102146
- Swagger4WCF: https://www.codeproject.com/Tips/1190441/How-to-generate-basic-swagger-yaml-description-for
103147
- NuGet Swagger4WCF: https://www.nuget.org/packages/Swagger4WCF
104148
- Unity.WCF: https://www.devtrends.co.uk/blog/introducing-unity.wcf-providing-easy-ioc-integration-for-your-wcf-services
149+
- Set StatusCode: https://codeblitz.wordpress.com/2009/04/27/how-to-host-and-consume-wcf-restful-services/
150+
- API Best Practices: https://developers.redhat.com/blog/2017/01/19/applying-api-best-practices-in-fuse/#
105151

106152
The End

WebApplicationWcfRest1/RestService1.svc.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.ServiceModel.Activation;
3+
using System.ServiceModel.Web;
34
using WebApplicationWcfRest1.interfaces;
45
using WebApplicationWcfRest1.models;
56

@@ -12,6 +13,10 @@ public class RestService1 : IBookService
1213
public void AddBook(Book book)
1314
{
1415
Debug.WriteLine(book.Name);
16+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created; // 201
17+
if (book.Name == "The incredible stamp") { // Book exist
18+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Conflict; // 409
19+
}
1520
}
1621

1722
public void DeleteBook(string id)
@@ -21,7 +26,12 @@ public void DeleteBook(string id)
2126

2227
public Book GetBookById(string id)
2328
{
24-
return new Book() {Id = 1, Name= "The incredible stamp" };
29+
if (id == "2") { // Book does not exist - 404
30+
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Resource not found");
31+
return null;
32+
} else {
33+
return new Book() { Id = 1, Name = "The incredible stamp" };
34+
}
2535
}
2636

2737
public Book[] GetBooksList()
@@ -32,6 +42,11 @@ public Book[] GetBooksList()
3242
public void UpdateBook(Book book)
3343
{
3444
Debug.WriteLine(book.Name);
45+
if (book.Id == 2) { // Book does not exist - 404
46+
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Resource not found");
47+
} else if (book.Name == "") { // Invalid request
48+
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.MethodNotAllowed; // 405
49+
}
3550
}
3651
}
3752
}

0 commit comments

Comments
 (0)