29 posts
Posted 01 February 2014 - 08:09 PM
how do I use http.post?
for pastebin, will this work?
I want it to login and return a user-key.
devkey = 033ab381c6c51fab106c0317a8ee91a6
function login(user,password)
h = http.post("http://pastebin.com/api/api_login.php/","api_dev_key="..devkey.."&api_user_name="..user.."&api_user_password="..password)
userkey = h.getResponseCode()
return userkey
end
8543 posts
Posted 01 February 2014 - 08:21 PM
You might try looking through the pastebin program to see what it does, or Cranium's SmartPaste program, as I think his uses logins.
29 posts
Posted 01 February 2014 - 08:23 PM
… Thanks, but I wanna code this my way.
Just wanna ask how to use http.post() and h.getResponseCode()
8543 posts
Posted 01 February 2014 - 08:36 PM
It wouldn't be the response code, it would be in the actual response. I was pointing out existing code so that you could use it as a reference, to see how it works in existing, working code. Nothing about that means that you can't "code it [your] way". For reference, Cranium's implementation looks vaguely thus, stripping out some of the extraneous things:
local response = http.post(
"http://pastebin.com/api/api_login.php",
"api_dev_key="..devKey.."&"..
"api_user_name="..textutils.urlEncode(username).."&"..
"api_user_password="..textutils.urlEncode(password)
)
local userKey = response.readAll()
Note that it has nothing to do with the getResponseCode call.
1281 posts
Posted 01 February 2014 - 08:41 PM
the http functions work the same as the fs functions mostly. As for creating a login system and such, give this a read
http://pastebin.com/api
29 posts
Posted 01 February 2014 - 08:55 PM
It wouldn't be the response code, it would be in the actual response. I was pointing out existing code so that you could use it as a reference, to see how it works in existing, working code. Nothing about that means that you can't "code it [your] way". For reference, Cranium's implementation looks vaguely thus, stripping out some of the extraneous things:
local response = http.post(
"http://pastebin.com/api/api_login.php",
"api_dev_key="..devKey.."&"..
"api_user_name="..textutils.urlEncode(username).."&"..
"api_user_password="..textutils.urlEncode(password)
)
local userKey = response.readAll()
Note that it has nothing to do with the getResponseCode call.
Umm… so you mean it returns a value and you use it directly as a variable?
the http functions work the same as the fs functions mostly. As for creating a login system and such, give this a read
http://pastebin.com/api
I read it.
3790 posts
Location
Lincoln, Nebraska
Posted 01 February 2014 - 09:06 PM
Yes, when using the http.post() command in ComputerCraft, the website has the option to send a reply to the POST command. This is read by ComputerCraft, and in this specific instance, is saved as a string value in the variable 'response'. From there, you can use that response any way you need to.
The example above is exactly what you would need to use to login to Pastebin, and return a user key. It's almost exactly what you would use(except certain syntax) if you were using PHP to login.
Edited on 01 February 2014 - 08:14 PM
8543 posts
Posted 01 February 2014 - 09:08 PM
For clarity, I moved the .readAll(), Cranium. The response recieved from http.post is similar to a file handle. Use readAll() to read out the entire contents as a string, as you can see is done with response being read into userKey above.
29 posts
Posted 01 February 2014 - 09:59 PM
OK, so heres my code…
function pastebin.post(devkey,user,pass)
resp = http.post(
"api_dev_key="..devkey.."&api_user_name="..user.."&api_user_pass="..pass
)
usrkey = resp.readAll()
return usrkey
end
Edited on 01 February 2014 - 08:59 PM
8543 posts
Posted 01 February 2014 - 10:49 PM
That's not going to work, you've chopped off the whole pastebin.com part of the URL.
29 posts
Posted 01 February 2014 - 11:17 PM
What about now?
function pastebin.post(devkey,user,pass)
resp = http.post(
"http://pastebin.com/api/api-login.php","api_dev_key="..devkey.."&api_user_name="..user.."&api_user_pass="..pass
)
usrkey = resp.readAll()
return usrkey
end