Skip to content
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

Executing HTTP request followed by another HTTP request #1258

Closed
adamdyga opened this issue Apr 19, 2016 · 4 comments
Closed

Executing HTTP request followed by another HTTP request #1258

adamdyga opened this issue Apr 19, 2016 · 4 comments

Comments

@adamdyga
Copy link

I wanted to execute a HTTP request (using http module) and when a response comes, immediately execute another request. The code is more or less is like this:

url = "http://google.com"

function firstRequest(callback)
  print("First request, connecting to " .. url)
  http.get(url, nil,
    function(code, data)
        print("First response: " .. code .. ", data: " .. data)
        callback()
    end)
end

function secondRequest()
  print("Second request, connecting to " .. url)
  http.get(url, nil,
    function(code, data)
        print("Second response: " .. code .. ", data: " .. data)
    end)
end


firstRequest(secondRequest)

Unfortunately the second request is never executed, in the output I always get:

First request, connecting to http://google.com
> First response: 302, data: ....
Second request, connecting to http://google.com

The response from the second request never comes back. What am I doing wrong?

I suspect that when the callback function of the first request is being executed it is not possible to "initiate" another HTTP request. If it is the cause, what is the proper way of achieveing such simple sequence of requests?

@marcelstoer
Copy link
Member

I suspect that when the callback function of the first request is being executed it is not possible to "initiate" another HTTP request.

I never tried myself but why not, have you tried? This all being asynchronous and event-driven I'd expect that to work just fine.

@adamdyga
Copy link
Author

adamdyga commented Apr 21, 2016

There is output of the script I attached, so don't you think it's very likely that I tried? 😉
If the callback is not an issue, what has to be done to make the above script work?

@marcelstoer
Copy link
Member

marcelstoer commented Apr 21, 2016

Grrr... mea culpa, sorry about that. I didn't study your example carefully enough and particularly had missed the last line (at that time I didn't think it was a complete example). Pity this seems to fall under #719 and should be discussed elsewhere.

If the callback is not an issue, what has to be done to make the above script work?

Replace callback() with node.task.post(callback) for example -> http://nodemcu.readthedocs.org/en/dev/en/modules/node/#nodetaskpost

@adamdyga
Copy link
Author

adamdyga commented May 2, 2016

Hi @marcelstoer,

Thanks for your hint, it works!
I'm just wondering what is the explanation of such behaviour? I would like to avoid such pitfalls in the future...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants