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

http.post() to Pastebin throws "Invalid api_option"

Started by Windows10User, 09 May 2018 - 01:37 PM
Windows10User #1
Posted 09 May 2018 - 03:37 PM
I am working on a Pastebin API (basically the pastebin program, but way better). When I went out to test the upload function, it threw "Bad API request, invalid api_option". I set it to "paste" just like Pastebin says, but I still get that.

Upload function:

function upload(file, title, user, pass)
   if not http then
	  return false, "HTTP required"
   end

   if fs.isDir(file) then
	  return false, "\"file\" is a directory"
   end

   if fs.exists(file) == false then
	  return false, "\"file\" doesn't exist"
   end

   local userkey = ""

   if user and pass then
	  local uresponse = http.post("https://www.pastebin.com/api/api_login.php",
		 "api_dev_key=24eeeaa32fe9c4724e7e4f4f4c601f80&"..
		 "api_user_name="..textutils.urlEncode(user).."&"..
		 "api_user_password="..textutils.urlEncode(pass)
	  )

	  if uresponse then
		 if string.match(uresponse.readAll(), " ") == nil then
			userkey = uresponse.readAll()
		 end
		 uresponse.close()
	  end
   end

   local hdata = fs.open(file, "r")
   local data = hdata.readAll()
   hdata.close()

   local presponse = http.post("https://www.pastebin.com/api/api_post.php",
	  "api_option=paste&"..
	  "api_dev_key=CENSORED&"..
	  "api_paste_code="..data.."&"..
	  "api_user_key="..userkey.."&"..
	  "api_paste_name="..title and title or "".."&"..
	  "api_paste_format=lua&"..
	  "api_paste_expire_date=N&"..
	  "api_paste_private=0"
   )

   if presponse then
	  local presponses = presponse.readAll()
	  presponse.close()
	  return string.sub(presponses, 21)
   end
end
Lupus590 #2
Posted 09 May 2018 - 05:54 PM
The builtin pastebin program use textutils.urlEncode for the paste name and code.

https://github.com/d...astebin.lua#L63
Edited on 09 May 2018 - 03:54 PM
Windows10User #3
Posted 09 May 2018 - 06:07 PM
Yeah, I looked at the source code a few times and that clearly slipped past my eyes. Will try it out!

EDIT: Nope, still invalid api_option. The postData from the pastebin program works, tho…
Edited on 09 May 2018 - 04:08 PM
Windows10User #4
Posted 09 May 2018 - 06:28 PM
BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!
EveryOS #5
Posted 09 May 2018 - 06:36 PM
BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!

It's called a pattern, it is to return part of a string

http://lua-users.org/wiki/PatternsTutorial
Windows10User #6
Posted 09 May 2018 - 07:28 PM
BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!

It's called a pattern, it is to return part of a string

http://lua-users.org...atternsTutorial

I know it's a pattern, but why random characters?
SquidDev #7
Posted 09 May 2018 - 07:53 PM
So [^/] means any character which isn't a /. + means one or more, and $ means the end of the string. So when you provide a string like foo/bar/baz, it'll match as many non-/ characters at the end of the string - namely baz. In this particular case, I believe pastebin returns http://pastebin.com/PASTE_ID in its response, and so it strips the ID out of that.
Lupus590 #8
Posted 10 May 2018 - 01:01 PM
Have you tried looking at the APi docs? https://pastebin.com/api
Windows10User #9
Posted 10 May 2018 - 01:24 PM
Yeah, I literally studied it and the built-in pastebin app.
Edited on 10 May 2018 - 11:24 AM