Jump to content




Troubles with http.post


  • You cannot reply to this topic
8 replies to this topic

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 16 January 2013 - 08:55 AM

I'm back, working on my SmartPaste program again, but I am having trouble with some of the code. When I try to login, I get a response from Pastebin, but it's blank. I need it to provide a user key when I am logged in successfully, but it won't do that. I copied over my login code from the existing SmartPaste, but for some reason, it won't work....
Can anyone tell me why?
local function loginScript()
paintutils.drawLine(10,6,40,6, colors.blue)
term.setCursorPos(10, 6)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.blue)
write("Please login")
for i = 1, 7 do
  paintutils.drawLine(10, 6 + i, 40, 6 + i, colors.lightGray)
end
local loginObjects = {
  {name = "Username:",
  nameX = 11,
  nameY = 9,
  color = colors.black,
  BGcolor = colors.lightGray},
  {name = "Username:		 ",
  nameX = 21,
  nameY = 9,
  color = colors.white,
  BGcolor = colors.white},
  {name = "Password:",
  nameX = 11,
  nameY = 11,
  color = colors.black,
  BGcolor = colors.lightGray},
  {name = "Password:		 ",
  nameX = 21,
  nameY = 11,
  color = colors.white,
  BGcolor = colors.white}
  }
for i = 1, #loginObjects do
  term.setCursorPos(loginObjects[i].nameX, loginObjects[i].nameY)
  term.setTextColor(loginObjects[i].color)
  term.setBackgroundColor(loginObjects[i].BGcolor)
  write(loginObjects[i].name)
end

term.setCursorPos(22, 9)
while true do
  username = limitRead(16)
  if username ~= "TEMP_USER" then
   break
  end
end
term.setCursorPos(22, 11)
while true do
  password = limitRead(16, "*")
  if password ~= "TEMP_PASSWORD" then
   break
  end
end
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 badRequests = {
  "Bad API request, invalid login",
  "Bad API request, account not active"
  }
if response then
  local sReponse = response.readAll()
  response.close()
  term.setCursorPos(1,y-1)
  print(sResponse)
  if sResponse == badRequests[1] then
   term.setCursorPos(11,7)
   term.setBackgroundColor(colors.lightGray)
   term.setTextColor(colors.red)
   write("Invalid login!")
  end
end
end
This is just the login function. It's the only thing that is giving me trouble right now.

EDIT: Forgot to add my limitRead() function.... just in case you guys want to run the function.
-- limit read function
local function limitRead(limX, rChar)
term.setCursorBlink(true)
local origX, origY = term.getCursorPos()
local returnString = ""
while true do
  local xPos, yPos = term.getCursorPos()
  local event, p1, p2 = os.pullEvent()
  if event == "char" then
   returnString = returnString..p1
   if not rChar then
    if not limX then
	 term.setTextColor(colors.blue)
	 write(p1)
    else
	 if string.len(returnString) >= limX then
	  term.setTextColor(colors.blue)
	  term.setCursorPos(origX, origY)
	  write(string.sub(returnString, (string.len(returnString)-limX)+1))
	 elseif string.len(returnString) < limX then
	  term.setTextColor(colors.blue)
	  write(p1)
	 end
    end
   else
    if not limX then
	 term.setTextColor(colors.blue)
	 write(rChar)
    else
	 if string.len(returnString) >= limX then
	  term.setTextColor(colors.blue)
	  term.setCursorPos(origX, origY)
	  write(string.rep(rChar, limX))
	 elseif string.len(returnString) < limX then
	  term.setTextColor(colors.blue)
	  write(rChar)
	 end
    end
   end
  elseif event == "key" and p1 == 14 then --backspace
   returnString = string.sub(returnString, 1, (string.len(returnString))-1)
   term.setCursorPos(xPos-1,yPos)
   term.setTextColor(colors.blue)
   write(" ")
   term.setCursorPos(origX, origY)
   if string.len(returnString) >= limX then
    if not rChar then
	 term.setTextColor(colors.blue)
	 write(string.sub(returnString, (string.len(returnString)-limX)+1))
    else
	 term.setTextColor(colors.blue)
	 write(string.rep(rChar,limX))
    end
   else
    if not rChar then
	 term.setTextColor(colors.blue)
	 write(returnString)
    else
	 term.setTextColor(colors.blue)
	 write(string.rep(rChar, string.len(returnString)))
    end
   end
  elseif event == "key" and p1 == 15 then --tab
   break
  elseif event == "key" and p1 == 28 then --enter
   break
  end
end
term.setCursorBlink(false)
return returnString
end

Edited by Cranium, 16 January 2013 - 08:56 AM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 January 2013 - 11:25 AM

Can we also please have one of the dev keys to test? also whats the key that the user gets?

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 16 January 2013 - 11:46 AM

Oh, yeah. Forgot about that.
local devKey = "3575e50cc6635311078014a550bc3f9f"
That should get you going.
The user key should be a hexadecimal string taken from the table that http.post returns.

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 16 January 2013 - 05:40 PM

Oh wow, your limitRead function is much longer than mine :P

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 January 2013 - 05:43 PM

View PostCranium, on 16 January 2013 - 11:46 AM, said:

Oh, yeah. Forgot about that.
local devKey = "3575e50cc6635311078014a550bc3f9f"
That should get you going.
The user key should be a hexadecimal string taken from the table that http.post returns.
I cannot see what wrong at all... I've looked over it a few times and it doesn't seem like there should be an issue... just an empty table is returning............

#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 17 January 2013 - 05:26 AM

View PostTheOriginalBIT, on 16 January 2013 - 05:43 PM, said:

I cannot see what wrong at all... I've looked over it a few times and it doesn't seem like there should be an issue... just an empty table is returning............
I know, it should be returning a response. I just don't understand why it is not working.....

#7 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

Posted 17 January 2013 - 05:55 AM

Fixed!
(End of loginScript())
local response = http.post(
		 "http://pastebin.com/api/api_login.php", "api_dev_key="..textutils.urlEncode(devKey).."&api_user_name="..textutils.urlEncode(username).."&api_user_password="..textutils.urlEncode(password)
		 ).readAll()
local badRequests = {
  "Bad API request, invalid login",
  "Bad API request, account not active"
  }
if response then
  term.setCursorPos(1,1)
  term.setTextColor(colors.white)
  print(response)
  if sResponse == badRequests[1] then
   term.setCursorPos(11,7)
   term.setBackgroundColor(colors.lightGray)
   term.setTextColor(colors.red)
   write("Invalid login!")
  end
end
end
Not exactly sure where I fixed it, but I did. Pretty sure it had something to do with my moving of readAll().
If you are interested, I was testing with this.

#8 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 17 January 2013 - 06:24 AM

Thanks, Zeke! That really helped! I don't know why adding the .readAll() to the end worked, but it did!

#9 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

Posted 17 January 2013 - 06:26 AM

I think it has more something to do with not doing close().

But whatever. Your Welcome! (Remember mai credits)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users