Jump to content

[FIXED] fetchRemote postData - Am I doing it correctly?


Tails

Recommended Posts

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 by Guest
Link to comment

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...