Kevin Kuei
Kevin Kuei

Reputation: 193

how to make sure the http response was delivered?

To respond a http request, we can just use return "content" in the method function.

But for some mission-critical use cases, I would like to make sure the http 200 OK response was delivered. Any idea?

Upvotes: 6

Views: 4313

Answers (4)

Tarun Lalwani
Tarun Lalwani

Reputation: 146520

The HTTP protocol doesn't work that way. If you need an acknowledgement then you need the client to send the acknowledgement to you.

Or you should look at implementing a bi-direction socket (a sample library is socket.io) where the client can send the ACK. If it is mission critical, then don't let it be on just http, use websockets

Also you can use AJAX callbacks to gather acknowledgment. One way of creating such a solution would be UUID generated for every request and returned as a part of header

$ curl -v http://domain/url
....
response:

X-ACK-Token: 89080-3e432423-234234-23-42323

and then client make a call again

$ curl http://domain/ack/89080-3e432423-234234-23-42323

So the server would know that the given response has been acknowledge by the client. But you cannot enforce automatic ACK, it is still on the client to send it, if they don't, you have no way of knowing

PS: The UUID is not an actual UUID here, just for example shared as random number

Upvotes: 15

Jorge Sampayo
Jorge Sampayo

Reputation: 868

It is not possible with HTTP, if for some reason you can't use Sockets because your implementation requires HTTP (like an API) you must acknowledge a timeout strategy with your client.

It depends on how much cases you want to handle, but for example you can state something like this:

  1. Client generate internal identifier and send HTTP request including that "ClientID" (like a timestamp or a random number) either in the Headers or as a Body parameter.
  2. Server responds 200 OK (or error, does not matter)
  3. Client waits for server answer 60 seconds (you define your maximum timeout).
    1. If it receives the response, handle it and finish.
    2. If it does NOT receive the answer, try again after the timeout including the same "ClientID" generated in the step 1.
      1. Server detects that the "ClientID" was already received.
        • Either return 409 Conflict informing that it "Already exists" and the client should know how to handle it.
        • Or just return 200 OK and the client never knew that it was received the first time.

Again, this depends a lot on your business / technical requirements. Because you could even get two or more consecutive loops of timeout handle.

Hope you get an idea.

Upvotes: 1

Aleksandar
Aleksandar

Reputation: 2662

as @tarun-lalwani already written is the http protocol not designed for that. What you can do is to let the app create a file and your program checks after the 200 respone the existence and the time of the remote file. This have the implication that every 200 response requires another request for the check file

Upvotes: 0

Jesse Milam
Jesse Milam

Reputation: 66

Take a look at Microsofts asynchronous server socket.

An asynchronous server socket requires a method to begin accepting connection requests from the network, a callback method to handle the connection requests and begin receiving data from the network, and a callback method to end receiving the data (this is where your client could respond with the success or failure of the HTTP request that was made).

Example

Upvotes: 1

Related Questions