Jump to content

callRemote and fetchRemote


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 post
  • Moderators
11 minutes ago, Erlkonig said:

When I am doing that with fetchRemote, it returns the correct string but I need that the table ofc :D

Hi.

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

  • Like 1
Link to post
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 post
  • Moderators
2 minutes ago, Erlkonig said:

I tried but the table is empty while the string returns the information.

How the string looks like? paste it here

  • Like 1
Link to post
Just now, Patrick said:

How the string looks like? paste it here

Is the the syntax which I used is correctly? Just a moment, I'll send the picture.

tqtmGCK.png

The same HTTP request with firefox:
X08lwrX.png

Link to post
  • 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.mtasa.com/topic/130181-lua-tables-as-sets-instead-of-arrays/

 

  • Thanks 1
Link to post
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.mtasa.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 post

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