Jump to content

fetchRemote isn't POSTING.


King12

Recommended Posts

Hi, I'm trying to POST data of mapName, adminName and action. The php code is receiving the POST when I use Postman but not it doesn't receive anything from mta. I'm missing something?

 

-- Function to get currently running map resource
function getCurrentMapResource()
    for _, res in ipairs(getResources()) do
        if getResourceState(res) == "running" and getResourceInfo(res, "type") == "map" then
            return res
        end
    end
    return nil
end

-- Verify admin rights
function isAdmin(player)
    if not isElement(player) then return false end
    local account = getPlayerAccount(player)
    return account and isObjectInACLGroup("user."..getAccountName(account), aclGetGroup("Admin"))
end

-- Send HTTP request to PHP to move the map folder
function sendMoveRequest(mapName, action, player)
    local url = "http://localhost/move_map.php"
    local postData = toJSON({
        mapName = mapName,
        action = action,
        adminName = getAccountName(getPlayerAccount(player))
    })

    -- Debugging: Output the JSON data being sent
    outputDebugString("Sending JSON data: " .. postData)

    fetchRemote(url, {
        method = "POST",
        headers = { ["Content-Type"] = "application/json" },
        postData = postData
    }, function(responseData, responseInfo)
        -- Debugging: Output response details
        outputChatBox("Response Data: " .. tostring(responseData), player, 255, 0, 0)
        outputChatBox("Response Info: " .. toJSON(responseInfo, true), player, 255, 0, 0)

        if responseInfo.success and responseInfo.statusCode == 200 then
            local responseTable = fromJSON(responseData)
            if responseTable and responseTable.success then
                outputChatBox("Map " .. action .. "ed successfully!", player, 0, 255, 0)
                stopResource(getResourceFromName(mapName))
                refreshResources()
            else
                outputChatBox("Failed to " .. action .. " map: " .. tostring(responseTable.message), player, 255, 0, 0)
            end
        else
            outputChatBox("HTTP request failed. Status code: " .. tostring(responseInfo.statusCode), player, 255, 0, 0)
        end
    end)
end



-- Handle map acceptance/declination
function manageCurrentMap(action, player)
    if not isAdmin(player) then
        outputChatBox("Access denied!", player, 255, 0, 0)
        return
    end

    local currentMap = getCurrentMapResource()
    if not currentMap then
        outputChatBox("No map currently running!", player, 255, 0, 0)
        return
    end

    local mapName = getResourceName(currentMap)
    sendMoveRequest(mapName, action, player)
end

-- Command handlers
addCommandHandler("accept", function(player)
    manageCurrentMap("accept", player)
end)

addCommandHandler("decline", function(player)
    manageCurrentMap("decline", player)
end)

 

I'm getting these responses in debug:

Response Data: {"success":false,"message":"Missing required fields: adminName, action, mapName","receivedData":[{"adminName":"lego","mapName":"-dm-3bady-vol3-the-nature-essence","action":"accept"}]}
Failed to accept map: Missing required fields: adminName, action, mapName

Edited by King12
Link to comment
  • Moderators
40 minutes ago, King12 said:

Failed to accept map: Missing required fields: adminName, action, mapName

Are you sure your API is capable of accepting JSON?

fetchRemote(url, {
	method = "POST",
	formFields = {
		adminName = "",
		action = "",
		mapName = ""
	},
}, function(responseData, responseInfo)
        

 

If JSON is expected, see also this comment on the wiki page:

Quote

Warning: When using toJSON for submitting data, make sure to use string.sub(data, 2, -2) to remove the initial and final brackets, as many APIs will not understand the request

postData = string.sub(postData, 2, -2) 
Link to comment
47 minutes ago, IIYAMA said:

Are you sure your API is capable of accepting JSON?

fetchRemote(url, {
	method = "POST",
	formFields = {
		adminName = "",
		action = "",
		mapName = ""
	},
}, function(responseData, responseInfo)
        

 

If JSON is expected, see also this comment on the wiki page:

postData = string.sub(postData, 2, -2) 

I wasn't paying attention to that part in the wiki, It's working now. Thank you!

  • Like 1
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...