Attempt to call nil value {global PerformHttpRequest}

Hello,

I’m trying to send some data to a json file that gets added to a diffrent DB so we can use this data on our website. But i keep getting the error "Attempt to call a nil value {global ‘PerformHTTPRequest’}

The code is below:

RegisterCommand('112', function(source, args, raw)
    local url = "http://ip/url/filename.json"
    local args = table.concat(args, ' ') -- Group up all the info that is send with the exceutedcommand. 

    -- Get location of call
    local x,y,z = table.unpack(GetEntityCoords(PlayerPedId()))

    local streetname = exports[GetCurrentResourceName()]:reverseStreetHash(GetStreetNameAtCoord(x,y,z))
    local zone = exports[GetCurrentResourceName()]:reverseZoneHash(GetHashKey(GetNameOfZone(x, y, z)))
    local area = exports[GetCurrentResourceName()]:reverseAreaHash(GetHashOfMapAreaAtCoords(x, y, z))

    local locationString = string.format("%s, %s (%s)", streetname, zone, area)

    PerformHttpRequest(url, function(err, text, header) end, 
    'POST', 
    json.encode({username = source, content = args, location = locationString}), {["Content-Type"] = 'application/json'})

end, false)

-- Returns the error SCRIPT ERROR: @resourcename/client/client.lua 161 (this script) "Attempt to call a nil value {global 'PerformHTTPRequest'}

So a player uses /112 “Help my dog is drowning”. “Help my dog is drowning” is the new args and get sent to the HTTPRequest. But that isn’t working cause of the error.

Anyone can help me out with the error thanks!

Need more information, react under the topic :smiley:

PerformHttpRequest work only on server file, not on client file

Ah that’s kinda dump of me lol.

Fixed the error, but nothing shows up in the Json file. I have the following code added in the server.lua

AddEventHandler("livemap:sendJson", function(source, args, locationString, name)
    -- local call = args
    -- local loc = locationString
    print(name)
    print(locationString)
    print(args)

    local url = "http://localhost/statusupdate/jemoeder.json"

    PerformHttpRequest(url, function(err, text, header) end, 
    'POST', 
    json.encode({username = name, meldingsbericht = args, postcode = locationString, statusmelding = "open"}), {["Content-Type"] = 'application/json'})

    print(username)
    print(meldingsbericht)
    print(postcode)
    print(statusmelding)

end)

First couple of prints does print in the server console the rest is nil idk why but a few extra information :slightly_smiling_face:

you cant use PerformHttpRequest at client side

print(username)
print(meldingsbericht)
print(postcode)
print(statusmelding)

This always nil cause your define only in the encode. So in server console doesn’t existe
Prefer print text but in the function

PerformHttpRequest(url, function(err, text, header) 
    print(text)
end, 'POST',  json.encode({username = name, meldingsbericht = args, postcode = locationString, statusmelding = "open"}), {["Content-Type"] = 'application/json'})

Does this print have to print the username, meldingsbericht etc? Cause it’s nill that might explain the issue here.

Unfortunately, I don’t have much knowledge on this point…
You can start by checking that the server response is equal to 200
After decode response and print

something like this

PerformHttpRequest(url, function(status, text, header)
    if status == 200 then
        print(json.decode(text)
    end
end, 'POST', json.encode({username = name, meldingsbericht = args, postcode = locationString, statusmelding = "open"}), {["Content-Type"] = 'application/json'})