简体   繁体   中英

How do I use HTTP:PostAsync() to submit JSON data to my server? (ROBLOX)

So I understand how GetAsync works, and I have scripts on my server that work with what I'm trying to accomplish. But in terms of getting data to the server instead of reading it from a json, how would I do that? On the server side, my PHP Script that gets all the post data from the submitted JSON and checks a database with all the information and if its not there, it inserts it. (It's a ban system. But I'm not sure if I have roblox-side set up correctly or if i have server-side set up correctly. Here's my code:

This is running within Custom Adonis Admin System

SendWebBan = function(plr,UserID,Username,Agent,Comment)
            local http = game:GetService("HttpService")
            local Reason = "Banned in game by AAC Admin."
            local url = "URL HERE/gamebanlistener.php"
            local data = {
                ["UserID"] = UserID,
                ["Username"] = Username,
                ["Agent"] = Agent,
                ["Reason"] = Reason,
                ["Comment"] = Comment
            }

            local encode = http:JSONEncode(data)


            local reponse,err = pcall(function()
                http:PostAsync(url,encode)
            end)

            print(reponse)

            if reponse == "false" then
                Remote.MakeGui(plr,"Notification",{
                    Title = "Failed";
                    Message = "Ban Failed";
                    Duration = 10;
                })
            elseif reponse == "true" then
                Remote.MakeGui(plr,"Notification",{
                    Title = "Success";
                    Message = "Ban Successful";
                    Duration = 10;
                })
            else
                Remote.MakeGui(plr,"Notification",{
                    Title = "Failed";
                    Message = "Ban Failed";
                    Duration = 10;
                })
            end
        end;
<?php
    $ServerKey = "key here";
    $APIKey = $_POST["key"];
    $Data = json_decode($_POST['data']);
    $UserID = $Data.UserID;
    $Username = $Data.Username;
    $Agent = $Data.Agent;
    $Reason = $Data.Reason;
    $Comment = $Data.Comment;

    $Date = date("d/m/Y");
    if($APIKey == $ServerKey){
       //$conn = mysqli_connect($host,$dbuser,$dbpassword,$db);
        $Connection = mysqli_connect(sql credentials here);

    if($Connection->connect_errno)
    {
        echo('{"Status":"'. $Connection->connect_errno . '"}');
    }else {
        $BlacklResult = $Connection->query("SELECT * FROM bans"); //Blacklist is the name of my Bans database

        if($BlacklResult->num_rows > 0)
        {
            while($Row = $BlacklResult->fetch_assoc()){
                if($Row != null){
                    if($Row["UserID"] == $UserID){
                     echo 'Status:false';
                     return;
                    }else{
                     $Connection->query("INSERT INTO bans(UserID, Username, Agent, BanReason, BanComment,DateOfBan) VALUES(".$UserID.",".$Username.",".$Agent.",".$Reason.",".$Comment.",".$Date.");");
                     echo 'Status:true';
                    }
                };
            };
        };

        };
    }else {
        echo('{"Status":"API KEY"}');
    };
?>

When you pcall a function, the tuple that is returned is a bool followed by the result of the function. If the function throws an error, then result is replaced with the error string. Try making this change:

local success, result = pcall(function()
    return http:PostAsync(url,encode)
end)

print(success, result)

if success then
    -- request went through, parse the response from your endpoint
else
    -- parse the http error
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM