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

Http.post

Started by Nigma, 24 November 2013 - 02:09 PM
Nigma #1
Posted 24 November 2013 - 03:09 PM
This has probably been asked 100 times before. But since i searched these forums and the wiki and still can't figure it out. I'm sure this qualifies as a legitimate cry for help.

I've been messing with computercraft a while and absolutely love it. But right now i have a number of questions i can't seem to find the answer to. And they are basically all related to the HTTP API

I'm trying to setup some basic mysql connections, just for fun and messing around. But in order to get to that point i wanted to test out the HTTP functions first.
I run into the problem that i can't seem to verify if anything is actually happening. On the wiki it says the http.post() return a handle that i can use .readLine() on. This brings up a "Attempt to index ?" error on CC.

Does the http API still return a handle? How do i use it?
Since i am sending the http post to the same machine that has the server and php+mysql on it is there a way i can use "localhost" in the URL? Just "localhost" gives invalidURL error. Perhaps "http://localhost" ?

My current code looks like this:

local h = http.post("<url>","data1=bla&amp;data2=bla&amp;data3=bla")
repeat
  local line = h.readLine()
  print(line)
until not line
h.close

Using the PHP code:

<?php
if (isset($_POST['db']) &amp;&amp; isset($_POST['usr']) &amp;&amp; isset($_POST['pwd']){
  $database = urldecode($_POST['db']);
  $username = urldecode($_POST['usr']);
  $password = urldecode($_POST['pwd']);

  print "Provided information:<br />";
  print "Connecting to: " . $database . "<br />";
  print "Using data: " . $username . "/" . $password;
}
else {
  print "Please provide Database, Username and Password.";
}
?>

(I know the current code would be useless. This is how i bug test my own code.)
Lyqyd #2
Posted 24 November 2013 - 03:43 PM
Have you enabled the HTTP API in the config file?
Nigma #3
Posted 24 November 2013 - 04:04 PM
I believe so. It came like this by default. (Using the Direwolf20 1.5 FTB pack) From the server/config/ComputerCraft.cfg

B:enableAPI_http=true
Niels Henriksen #4
Posted 25 November 2013 - 04:34 AM
You need to use http://localhost or http://[serverip] to call the webserver
Nigma #5
Posted 26 November 2013 - 07:48 AM
Thanks, http://localhost works. Do you know if the http.post functions still return something? And if they do how to use it?
Even after another day of trial and error, i still only have error.
Magik6k #6
Posted 26 November 2013 - 08:36 AM
The "Attempt to index ?" means that you tried to access some structure(or in fact - table) field from nil variable. The http.post function will return nil when it fails1, so that you must check if you have correct response. To do so you may use if :

if handle then
  --handle is correct, use it
else
  --handle is nil, print some error message
end
If the http.post keeps returning nil, you may check few things:
  • is the port 80 opened?
  • does the requests actually come to server?
  • what is the http response code?
Edited on 26 November 2013 - 10:47 AM
Nigma #7
Posted 26 November 2013 - 12:42 PM
If the http.post keeps returning nil, you may check few things:
  • is the port 80 opened?
  • does the requests actually come to server?
  • what is the http response code?

Thanks that bit of code got me further. Still not able to get it to work though. I never thought about checking if the http.post() would return nil if it failed. Derp on my end…
  • Port 80 is open, i am running a blog/website on the same address for Overviewer. And that works so it is safe to assume port 80 is open. (?)
  • I don't know how to check if the request makes it to the server. I'm guessing there must be an Apache log somewhere? I'm running a linux server and am still quite new to it. Where would such a log be located?
  • I don't think i should get a response code unless it actually connects. (?)
Edited on 26 November 2013 - 11:53 AM
Magik6k #8
Posted 26 November 2013 - 05:28 PM
Logs from apache by default are located under /var/log/httpd or /var/log/apache2.
Nigma #9
Posted 26 November 2013 - 11:06 PM
Thank you, thank to the logs I found out my computercraft was correct all this time. I was simply missing a ) in the php side.
Curse php syntax errors! Everthing is working now, thanks for the help!