King12 Posted March 9, 2025 Posted March 9, 2025 (edited) 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 March 9, 2025 by King12
Moderators IIYAMA Posted March 9, 2025 Moderators Posted March 9, 2025 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) Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
King12 Posted March 9, 2025 Author Posted March 9, 2025 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! 1
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