This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
RoD's profile picture

HTTP Help

Started by RoD, 19 June 2014 - 05:09 PM
RoD #1
Posted 19 June 2014 - 07:09 PM
Hey, i had tried the HTTP api in computercraft, it is working fine to websites like twitter, youtube and google. I need to connect to this website: http://textuploader.com and i can't…. I get nil response from the webstie. Anyone knows what's up?
BTW: The http api needs some in depth tutprials, this has a lot of potential and i think the wiki info and some forum posts are not enough.
Lignum #2
Posted 19 June 2014 - 07:54 PM
I can connect to it without any issues. You should try again. Or perhaps it's a certain page you're having trouble with and not the actual site? It would help if you posted the exact URL you're connecting to.
RoD #3
Posted 19 June 2014 - 08:18 PM
I actually am trying to connect to the api page:
http://api.textuploader.com/v1
But i had tryed the main page as well:
http://textuploader.com/
Nothing…

BTW: Does it worth mention i am in ccemu..?
Edited on 19 June 2014 - 06:24 PM
Lyqyd #4
Posted 19 June 2014 - 08:28 PM
Try it in minecraft.
Lignum #5
Posted 19 June 2014 - 08:29 PM
Ah yes, I just tried it in the actual game. I was using cclite before. It seems that the site doesn't want connections from Java. To solve it, just pretend you're not connecting from Java by specifying a user agent.


local response = http.get("http://textuploader.com", { ["User-Agent"] = "YourProgramsNameOrSomethingThatIsntJava" })
print(response.readLine()) --# Should work now.
response.close()
RoD #6
Posted 19 June 2014 - 09:01 PM
Ah yes, I just tried it in the actual game. I was using cclite before. It seems that the site doesn't want connections from Java. To solve it, just pretend you're not connecting from Java by specifying a user agent.


local response = http.get("http://textuploader.com", { ["User-Agent"] = "YourProgramsNameOrSomethingThatIsntJava" })
print(response.readLine()) --# Should work now.
response.close()

Yup, it worked.
How did you know the problem was about java? Why this webstie and not google? And what are those arguments (User-Agent)? It seems usefull and i would like to learn mor about it. Sorry for so many question, i guess i am a curious person. Thanks :D/>
Edited on 19 June 2014 - 07:03 PM
Lignum #7
Posted 19 June 2014 - 09:17 PM
Yup, it worked.
How did you know the problem was about java? Why this webstie and not google? And what are those arguments (User-Agent)? It seems usefull and i would like to learn mor about it. Sorry for so many question, i guess i am a curious person. Thanks :D/>

Some websites like to block connections from Java clients. I have no idea why they do this but they do.
User-Agent is a HTTP header, which tells a website what program the connection was established with (e.g. the browser). You can find a list of HTTP headers here.
RoD #8
Posted 19 June 2014 - 09:37 PM
Yup, it worked.
How did you know the problem was about java? Why this webstie and not google? And what are those arguments (User-Agent)? It seems usefull and i would like to learn mor about it. Sorry for so many question, i guess i am a curious person. Thanks :D/>

Some websites like to block connections from Java clients. I have no idea why they do this but they do.
User-Agent is a HTTP header, which tells a website what program the connection was established with (e.g. the browser). You can find a list of HTTP headers here.
Ok thnks :D/>
Quick note: it wont work in ccemu. Maybe the version its too old? I guess i coul try to get the http api from a newest version of computercraft into the ccemu rom, but i guess the problem isn't in the http api…
Edit: I just realized there is no http api… lol stupid me XD
Edited on 19 June 2014 - 07:40 PM
Lignum #9
Posted 19 June 2014 - 09:40 PM
Ok thnks :D/>
Quick note: it wont work in ccemu. Maybe the version its too old? I guess i coul try to get the http api from a newest version of computercraft into the ccemu rom, but i guess the problem isn't in the http api…

Yes, the HTTP API is probably too old. You should be using the new version of CCEmu, CCEmuRedux, anyway.
Edited on 19 June 2014 - 07:41 PM
RoD #10
Posted 19 June 2014 - 09:42 PM
Ok thnks :D/>
Quick note: it wont work in ccemu. Maybe the version its too old? I guess i coul try to get the http api from a newest version of computercraft into the ccemu rom, but i guess the problem isn't in the http api…

Yes, the HTTP API is probably too old. You should be using the new version of CCEmu, CCEmuRedux, anyway.
Wow, thomas is making a new emulator? :D/> Awesome
RoD #11
Posted 20 June 2014 - 06:08 PM
Ok, i am having some troubles.
I want to connect to this page:
http://api.textuploader.com/v1/posts
In the browser it works, but when i try to do it in computercraft:
This one i get the same error as before (attempt to index ? (a nil value)):

local response = http.get("http://api.textuploader.com/v1/posts", { ["User-Agent"] = shell.getRunningProgram() })
This one (wich is the one i want to use) just freeze (it loops):

local response = http.post("http://api.textuploader.com/v1/posts","X-TextUploader-API-Key:"..key, { ["User-Agent"] = shell.getRunningProgram() })
I want to get the response from the posts page and i can't get anything…
Lignum #12
Posted 20 June 2014 - 06:44 PM
The former will respond with 401 Unauthorised, which makes ComputerCraft return nil, so that's normal.
Anyway, the main issue lies in the latter piece of code:
The documentation says: "To identify yourself, you send the following HTTP header with your API key". Which means that the second argument you're passing has to be inside the header table.
Edited on 20 June 2014 - 04:45 PM
RoD #13
Posted 20 June 2014 - 07:27 PM
For that i skip the second arument of the http.post and do something like:

local response = http.post("http://api.textuploader.com/v1/posts",_, { ["User-Agent"] = shell.getRunningProgram(), ["X-TextUploader-API-Key:"] = key })

?
Edited on 20 June 2014 - 05:27 PM
Lignum #14
Posted 20 June 2014 - 07:34 PM
For that i skip the second arument of the http.post and do something like:

local response = http.post("http://api.textuploader.com/v1/posts",_, { ["User-Agent"] = shell.getRunningProgram(), ["X-TextUploader-API-Key:"] = key })

?

You don't need a colon here:

["X-TextUploader-API-Key:"]
The second argument is required in http.post, so you can't skip it. Since you don't need post data, just use http.get.
Edited on 20 June 2014 - 05:34 PM
RoD #15
Posted 20 June 2014 - 07:58 PM
For that i skip the second arument of the http.post and do something like:

local response = http.post("http://api.textuploader.com/v1/posts",_, { ["User-Agent"] = shell.getRunningProgram(), ["X-TextUploader-API-Key:"] = key })

?

You don't need a colon here:

["X-TextUploader-API-Key:"]
The second argument is required in http.post, so you can't skip it. Since you don't need post data, just use http.get.
This one returns nil:

local response = http.get("http://api.textuploader.com/v1/posts", { ["User-Agent"] = shell.getRunningProgram(), ["X-TextUploader-API-Key"] = key })
Lignum #16
Posted 20 June 2014 - 08:05 PM
This one returns nil:

local response = http.get("http://api.textuploader.com/v1/posts", { ["User-Agent"] = shell.getRunningProgram(), ["X-TextUploader-API-Key"] = key })
Hmm, that should work. I don't have any experience with TextUploader, so I can't help you there, especially because ComputerCraft doesn't give you much detail about what exactly fails. I'm assuming it's the authorisation that fails (the api key in particular) and not the actual connection, so you should check the TextUploader docs to see if you're missing anything.
RoD #17
Posted 20 June 2014 - 08:11 PM
But even that the authorization fails, shouldn't i get this page:
http://api.textuploader.com/v1/posts ?
Lignum #18
Posted 20 June 2014 - 08:21 PM
But even that the authorization fails, shouldn't i get this page:
http://api.textuploader.com/v1/posts ?
No, ComputerCraft returns nil when something doesn't respond with a 200 OK response.
RoD #19
Posted 20 June 2014 - 08:35 PM
But even that the authorization fails, shouldn't i get this page:
http://api.textuploader.com/v1/posts ?
No, ComputerCraft returns nil when something doesn't respond with a 200 OK response.
hum..
Are this parameters required:
-H "Accept: application/json" \
-H "Content-Type: application/json" \
?
Edited on 20 June 2014 - 06:40 PM
Lignum #20
Posted 20 June 2014 - 08:45 PM
hum..
Are this parameters required:
-H "Accept: application/json" \
-H "Content-Type: application/json" \
?
No, they're not.
RoD #21
Posted 20 June 2014 - 09:20 PM
hum..
Are this parameters required:
-H "Accept: application/json" \
-H "Content-Type: application/json" \
?
No, they're not.
Pretty strange.
Thanks anyways, i learned a lot :D/>