New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple http.request
s to the same url cannot be differentiated
#629
Comments
For what it's worth, the way we usually solve it (and have in other Krist implementations) is by using URL fragments to attach an incrementing/random identifier to the end of the URL, like: local url = "https://krist.ceriat.net/addresses#123"
http.request(url)
local event, u = os.pullEvent("http_success")
if u == url then
-- This is the correct request
end Of course, in a serious implementation you'd probably use a table of URLs mapped to response handlers or something. PS: this trick works for websockets too |
Yeah. I've been discussing this for years, but really not a fan of it. It probably is the only feasible solution, but chucking more things in the event feels a little ugly too. |
Wouldn’t replacing the URL and adding a numerical ID keep the same number of event arguments? |
Yes, but it would break every program which uses |
Unless there's a use case for the URL that's returned by the event outside of comparing it with the initial requested URL, I don't think that every program that uses it directly would break, would it? |
Unfortunately a great many would, mine included. I usually "build" my urls and then compare the result to what was returned (allows for quickly changing what url we use by only changing one thing), so like: local urlFormat = "https://%s/%s"
local website = "krist.ceriat.net"
local endpoint = "transactions/new"
local builtUrl = string.format(urlFormat, website, endpoint)
http.request(builtUrl, --[[...]])
local event, url, body = os.pullevent("http_success")
if url == builtUrl then
--[[...]] If |
When making multiple requests using
http.request
to the same url, the response given in thehttp_success
/http_failure
events cannot be differentiated, causing some problems.As an example, using my KristWrap (Just for the
toKristWalletFormat
method and thejson.encode
) along with its recommended json/sha libraries, I can send two POST requests to krist's 'make a transaction' endpoint. If one of those fails, which failed? I cannot know.Using the following code which sends two async requests and then listens for responses, we can show this problem in action:
And responses,
output1
:output2
:We can see one of them failed, but which one? We can assume the first response coordinates with the first request, but what if timing things happened and the first response was actually to the second request?
I recommend adding a numerical id, returned by
http.request
that we can use in a comparison fromhttp_success
to determine the specific request id something is responding to, for example:The text was updated successfully, but these errors were encountered: