Having trouble getting a .json file using luasocket (inexperienced with this...)

Posted by Rivius on Mon 20 Aug 2012 11:58 AM — 3 posts, 15,846 views.

#0
I have been attempting to get the file: api.lusternia.com/characters.json using luasocket, but it would seem whenever I try, what gets downloaded says that the server does not recognize the request I am sending.

I believe it might have to do with what I am sending:


"GET api.lusternia.com/characters.json HTTP/1.0\r\n\r\n"


With the host being api.lusternia.com and the port being 80.

Is there something else I should be sending instead? I'm mostly following from a tutorial I've seen, so I'm very unfamiliar with this. I can download regular html files just fine off other sites using this method.


Edit:

A friend had told me that the following worked for them:

"GET "..file.." HTTP/1.1\r\nHost: "..host.."\r\nAccept: text/json\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0\r\n\r\n"


On attempting this, I receive this error:

HTTP/1.1 400 Bad Request
Date: Mon, 20 Aug 2012 17:22:41 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

Is there anything I can do about this?
Amended on Mon 20 Aug 2012 05:23 PM by Rivius
Australia Forum Administrator #1
This seemed to pull in the page for me:


http = require "socket.http"

page, result = http.request "http://api.lusternia.com/characters.json"

print (page)


#2
Aye. I was attempting to avoid using http request since I could not make it non-blocking. After playing around quite a lot, this managed to work for me (and nothing less):

c:send("GET "..file.." HTTP/1.1\r\nUser-agent: "..socket._VERSION.."\r\nHost: "..host.."\r\nConnection: close\r\n\r\n")

I also had to change my receive function a bit because at the time I was using receive("*l"), but this was less than useful. Instead I simply went with receive(), but concatenated the third return value where appropriate.

thanks!