5

I am building a REST API using ASP.NET Web API. I have a post request that may take more than 30s to complete. I resolved this by returning a 200 OK and a status attribute that can be polled by client app. If the status becomes "complete" the client can retrieve this result. But i am doing this mostly in any similar operation in the API.

I discovered the 102 HTTP Status Code that tells the client that the request is "Processing" and that it should wait for the complete response.

Questions : 1-What is your advice for using this approach ? 2-How to implement it in ASP.NET Web API using C#?

Edit: For more precise explanation i want to implement this sequence diagram on ASP.NET. Sequence Diagram To Implement

Anis Tissaoui
  • 834
  • 1
  • 7
  • 26
  • will you return the 102 immediately, or would you wait let's say 10 seconds on server-side and when your request thread detects that the operation is still running, end the request with the 102, cache the result on the server and wait for the client to send another request to fetch the result? – Cee McSharpface Sep 22 '17 at 10:52
  • @dlatikay I need to browse an azure blob storage, the browsing depends on a depth parameter which limits the recursive browsing of the file system in azure blob storage account. so i need to check if the request is taking longer than expected. – Anis Tissaoui Sep 22 '17 at 10:57
  • You can find the answer on how to return a specific error code here (I can't flag as duplicate as it doesn't have an accepted answer); HttpResponseMessage Create HttpStatusCode 550 – DiskJunky Sep 22 '17 at 10:59
  • 2
    Sending back 102 should be the end of the request, it's up to the client to keep asking until they get a valid (e.g. 200) response. – DavidG Sep 22 '17 at 11:00
  • 1
    @DavidG Yea, exactly there is no way for the client to wait for the same request. – Anis Tissaoui Sep 22 '17 at 11:03
  • 1
    @DiskJunky It is not a duplicate, i know how to return a specific status code. I am asking about the best way on how to do it. – Anis Tissaoui Sep 22 '17 at 11:05
  • except maybe with a technology like SignalR where the channel is kept open and the server can push data to the client. – Cee McSharpface Sep 22 '17 at 11:06
  • Taken from [RFC2518](https://httpstatuses.com/102) "The server MUST send a final response after the request has been completed." It doesn't sound like the client should request the result until it gets 200. – Anis Tissaoui Sep 22 '17 at 11:10
  • 3
    [related discussion](https://softwareengineering.stackexchange.com/q/316208) on software engineering SE – Cee McSharpface Sep 22 '17 at 11:18
  • Why not return status code 202 Accepted with a key that identifies the request. You can then kick off some background work and have a different endpoint which clients can query (with the key) to see how the request is going. – peco Sep 29 '17 at 06:59
  • @peco I am returning a 201 Created with a status property indicating the status of the operation. The client can pool the status like this : GET : api/dataset/{id}/status – Anis Tissaoui Sep 29 '17 at 09:36
  • "Sending back 102 should be the end of the request, it's up to the client to keep asking until they get a valid (e.g. 200) response" -- i don't think this is correct. The RFC spec says that the 102 is the signal to the client to wait because the request will take a long time. The server is still required to complete the request -- that's the part that isn't clear how to do in webapi because this is actually WebDav behavior. – Sinaesthetic Feb 04 '20 at 22:08

0 Answers0