Jump to content

callRemote and fetchRemote


Erlkonig

Recommended Posts

Hello everyone! Im trying to solve this problem already 3rd day but no results.
I am calling HTTP request and it returns JSON. For example it works in browser (Firefox doing it better).

When Im trying to call that request with a callRemote, I got returned the table but when I list that with a iteration it's clear as I understand.
When I am doing that with fetchRemote, it returns the correct string but I need that the table ofc :D

I am sure that I am doing something wrong but I do not know what exactly I am doing wrong. Thanks for everyone who will reply. 

Edited by Erlkonig
Link to comment
28 minutes ago, Patrick said:

Hi.

You have to convert JSON to Lua array with fromJSON. (probably callRemote do it automatically)

Hi! Thanks for reply! I tried but the table is empty while the string returns the information. The code below:

function newResult(getupdates)
    if getupdates then
        outputConsole("Start of iteration")
        local jsonUpdates = fromJSON(getupdates)
        for i, v in ipairs(jsonUpdates) do
            outputConsole(i..": "..v)
        end
        outputConsole("End of iteration")
        outputConsole(getupdates)
    else
        outputDebugString("ERROR!", 3)
    end
end

fetchRemote(link, newResult)

 

Link to comment
  • Moderators

Okay, so you have 2 fields in the json. ok and result

function newResult(getupdates)
    if getupdates then
        outputConsole("Start of iteration")
    
        local jsonUpdates = fromJSON(getupdates)
        local ok          = jsonUpdates["ok"]
    
        -- is 'ok' true?
        if ok then
            -- yes it was successful, so loop trough results
            local result = jsonUpdates["result"]
      
            -- you can print out tables to debug console to check content, with:
            iprint(result)
      
            for i, v in ipairs(result) do
                outputConsole(i..": update_id: "..v["update_id"].." | message_id: "..v["message"]["message_id"])
            end
        end
        outputConsole("End of iteration")
        outputConsole(getupdates)
    
    else
        outputDebugString("ERROR!", 3)
    end
end

fetchRemote(link, newResult)

 

Here you can read about tables: https://forum.multitheftauto.com/topic/130181-lua-tables-as-sets-instead-of-arrays/

 

  • Thanks 1
Link to comment
9 minutes ago, Patrick said:

Okay, so you have 2 fields in the json. ok and result


function newResult(getupdates)
    if getupdates then
        outputConsole("Start of iteration")
    
        local jsonUpdates = fromJSON(getupdates)
        local ok          = jsonUpdates["ok"]
    
        -- is 'ok' is true?
        if ok then
            -- yes it was successful, so loop trough results
            local result = jsonUpdates["result"]
      
            -- you can print out tables to debug console to check content, with:
            iprint(result)
      
            for i, v in ipairs(result) do
                outputConsole(i..": update_id: "..v["update_id"].." | message_id: "..v["message"]["message_id"])
            end
        end
        outputConsole("End of iteration")
        outputConsole(getupdates)
    
    else
        outputDebugString("ERROR!", 3)
    end
end

fetchRemote(link, newResult)

 

Here you can read about tables: https://forum.multitheftauto.com/topic/130181-lua-tables-as-sets-instead-of-arrays/

 

Thank you very much! Also thanks for the topic with tutorial. It works already. I have to learn it!

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