Getting JSON data from an external source


I want to fetch some JSON info from an external source. And I’m looking forward to use this great work by akbooer;;topic=17499.0;attach=13117

But first I have to get the data :slight_smile:

The source demands a secure ssl/https connection and that a username/password is provided.

Here is what I’ve tried so far with no luck:

local http = require("socket.http")
http.TIMEOUT = 5
http.PORT = 9000 -- The port is configured to 9000 and not 433 as more usual w/ https. 

local result, status = http.request("https://user:pwd@")

This does not seem to work. If I look at result it returns: nil, while the variable status returns: Connection refused.

I suspect that the issue is with the the syntax that I’m using to specify the username/password. Or? If so, what is the right way to specify a username/password?

Any help appreciated :slight_smile:

Does the user:password@ syntax work with a regular browser? Are there any characters in the username or password that need to be URI escaped?

If you instead get one of those popup prompts from your browser when you visit the page then you probably have to get into the realm of authentication headers. You may need the full power of the long form Luasocket syntax to specify the headers.

You might get some insight by using a tool like Firebug to see what headers are in a successful request, and then re-creating those in Luasocket.

Edit: another possibility is that it’s not actually using HTTPS even that that is what you mentioned in the URL scheme. There is some special magic and an additional library to include if you want HTTPS. It was talked about here in the forum a few months ago.

No in fact it does not.

That is exactly what I get. I got a popup prompt when using a browser.

Where do I find more info and exampes of Luasocket? This is really not an area I know much about currently.

Thanks. That is very helpful :slight_smile:

Thanks again.


I want to fetch some JSON info from an external source. And I’m looking forward to use this great work by akbooer;;topic=17499.0;attach=13117[/quote]

Wow, that is some of prettiest Lua ever written!


I wish there was a good tutorial on using Luasocket. But there isn’t. The reference manual online is the only definitive document and it has few examples and little explanation of them. It also assumes knowledge of the ltn12 source/sink library which has even less good documentation. I was reduced to finding examples online through judicious googling, and lots of experimentation.

There’s quite a body of working Luasocket example code in Vera apps. Many plugins have their source published on For my part, the WeMo plugin and the UPnP event proxy plugin are two examples of HTTP clients using Luasocket.