Tails Posted January 27, 2016 Share Posted January 27, 2016 (edited) EDIT: Fixed, see the result in my next post I'm trying to send some data to the Pastebin API but it keeps returning the same thing no matter what I put in the postData argument. So I was wondering if anyone has had any experience with this particular API, or just sending data with fetchRemote in general. The issue is the postData argument. I feel like it's not being picked up by the API at all. The code is based off of the php example on this page: http://pastebin.com/api#1 local api_paste_code = 'Just some random text' local api_paste_code = urlEncode(api_paste_code) local postData = 'api_option=paste&api_dev_key='..api_dev_key..'&api_paste_code='..api_paste_code..'' function pastebin(source,cmd) outputChatBox(api_paste_code,source) fetchRemote('http://pastebin.com/api/api_post.php',2, function(data,err) if err == 0 then outputChatBox("Response: "..data,source) else outputChatBox("Error: "..err,source) end end,postData,false) end addCommandHandler("paste",pastebin) Any help is appreciated! Edited January 27, 2016 by Guest Link to comment
tosfera Posted January 27, 2016 Share Posted January 27, 2016 [just letting you know that I'm currently trying to rewrite the PHP script towards Lua for you and give it a test here and there. Hang on ] Link to comment
Tails Posted January 27, 2016 Author Share Posted January 27, 2016 (edited) [just letting you know that I'm currently trying to rewrite the PHP script towards Lua for you and give it a test here and there. Hang on ] Already found a fix I'm using MTA's cURL functions: https://wiki.multitheftauto.com/wiki/Mo ... rl_perform Everything's working fine now!! Will post the result soon Edited January 27, 2016 by Guest Link to comment
tosfera Posted January 27, 2016 Share Posted January 27, 2016 I was about to say so ya, the thing about callRemote is that it's not posting data and the api requires a POST instead of a GET. Link to comment
Tails Posted January 27, 2016 Author Share Posted January 27, 2016 Here's the result: local api_dev_key = 'api key here' local url = 'http://pastebin.com/api/api_post.php' function pastebin(source,cmd,...) if ... then local curl = curlInit(url) local api_paste_code = table.concat({...}," ") local api_paste_code = urlEncode(api_paste_code) if not curl then outputChatBox("Can't connect to "..url,source) return else curlSetopt(curl, CURLOPT_POST, true) curlSetopt(curl, CURLOPT_POSTFIELDS, 'api_option=paste&api_dev_key='..api_dev_key..'&api_paste_code='..api_paste_code..'') curlSetopt(curl, CURLOPT_RETURNTRANSFER, 1) curlSetopt(curl, CURLOPT_VERBOSE, 1) curlSetopt(curl, CURLOPT_NOBODY, 0) local result,data = curlPerform(curl) if result == CURLE_OK then outputChatBox(tostring(data),source) end curlClose(curl) end end end addCommandHandler("paste",pastebin) Found somewhere on the net: function urlEncode(str) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w %-%_%.%~])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now