Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depending the order, some itens in the RequestJSON wont go to the output. #52

Closed
pushline opened this issue Apr 1, 2023 · 16 comments
Closed

Comments

@pushline
Copy link

pushline commented Apr 1, 2023

I was using an API (https://ipinfo.io/) to check if the players IP were vpn/proxy, and so some other things with it. The problem is, if i do the callback on a certain way to retrieve the data, it does not work anymore. Changing the order wont resolve it and etc.

If i do the "privacy" object in the bottom, it wont work, and the "ret" error will be 1.
I was doing the test aswell in a weather api (https://openweathermap.org/api) and occurs the same thing.

My code:

CheckISP(playerid) // this is called in OnPlayerConnect
{
    new playerIP[16];
    GetPlayerIp(playerid, playerIP, sizeof(playerIP));

    client = RequestsClient("https://ipinfo.io/");

    new format_antivpn[250];
    format(format_antivpn, sizeof(format_antivpn), "64.145.93.228?token=MY_TOKEN"); // 37.120.244.100
    // custom IPS with vpn = true;

    RequestJSON(
		client,
		format_antivpn,
		HTTP_METHOD_GET,
		"OnGetAntiVPN"
	);

    targetPLAYER = playerid;
}

forward OnGetAntiVPN(Request:id, E_HTTP_STATUS:status, Node:node);
public OnGetAntiVPN(Request:id, E_HTTP_STATUS:status, Node:node)
{
    switch(status){
        case HTTP_STATUS_OK:{
            new Node:asn, Node:org, Node:privacy;

            new playerid = targetPLAYER;

            JsonGetObject(node, "privacy", privacy);
            JsonGetBool(privacy, "vpn", User[playerid][VPN]);
            JsonGetBool(privacy, "proxy", User[playerid][Proxy]);

            JsonGetString(node, "city", User[playerid][City]);
            JsonGetString(node, "country", User[playerid][Country]);
            JsonGetString(node, "timezone", User[playerid][TimeZone]);
            JsonGetString(node, "postal", User[playerid][Zip]);
            
            JsonGetObject(node, "asn", asn);
            JsonGetString(asn, "name", User[playerid][Isp]);

            JsonGetObject(node, "company", org);
            JsonGetString(org, "name", User[playerid][Org]);
        
            printf("vpn %s", User[playerid][VPN] ? "true" : "false");
            printf("proxy %s", User[playerid][Proxy] ? "true" : "false");

            printf("city %s", User[playerid][City]);
            printf("country %s", User[playerid][Country]);
            printf("timezone %s", User[playerid][TimeZone]);
            printf("postal %s", User[playerid][Zip]);
            printf("isp %s", User[playerid][Isp]);
            printf("org %s", User[playerid][Org]);
        }
        default: printf("%s", E_HTTP:STATUS:status);
    }
    return 1;
}
@pushline
Copy link
Author

pushline commented Apr 1, 2023

Forgot to add the output in the omp-server.exe.

image

@rettdev
Copy link

rettdev commented Apr 2, 2023

Comigo acontece a mesma coisa ao tentar consumir uma api de clima.

@Southclaws
Copy link
Owner

Are you saying that moving JsonGetObject(node, "privacy", privacy); to another line causes the code to fail?

@pushline
Copy link
Author

pushline commented Apr 6, 2023

Are you saying that moving JsonGetObject(node, "privacy", privacy); to another line causes the code to fail?

Yeah. If I put in the top, i cannot obtain the rest of the JSON. Only the privacy indexes.

@Southclaws
Copy link
Owner

Hmm that is very odd, I can't see any field mutations going on in the getter https://github.com/Southclaws/pawn-requests/blob/master/src/natives.cpp#L489-L515 any idea @ADRFranklin ?

@rettdev
Copy link

rettdev commented Apr 12, 2023

this is weird. the same thing happens to me when trying to consume this api:

https://openweathermap.org/api

@pushline
Copy link
Author

I'll do some tests tonight changing the order. Will put the code + output in imgur, just to not put so many pics here.

@ADRFranklin
Copy link
Collaborator

ADRFranklin commented Apr 24, 2023

I noticed this issue a while ago, when rewriting my geoip library, it seems in some cases it's not able to find the fields in the extracted and parsed json results. I never found time to debug it, and I don't currently have the time to debug it now.

If no one else is able to debug it, then you'll have to wait for me to get some free time.

Also I am linking my geoip library as I think it's more beneficial to you then what you have.
http://sharex.justmichael.xyz/fAFE9/XUBANAHi29.txt

@pushline
Copy link
Author

Understandable. I'll wait then, thanks.

@ADRFranklin
Copy link
Collaborator

@pushline after further investigation, it turns out that you need to turn off garbage collection for the initial node, as the second node you create causes the first one to be garbage collected and removed. So simply adding JsonToggleGC and setting the value to false after creating the node and then at the end of the function when done with it, toggle the gc back on.

forward OnGetAntiVPN(Request:id, E_HTTP_STATUS:status, Node:node);
public OnGetAntiVPN(Request:id, E_HTTP_STATUS:status, Node:node)
{
    switch(status){
        case HTTP_STATUS_OK:{
            new Node:asn, Node:org, Node:privacy;
           JsonToggleGC(node, false);

            new playerid = targetPLAYER;
          
            JsonGetObject(node, "privacy", privacy);
            JsonToggleGC(privacy, false);

            JsonGetBool(privacy, "vpn", User[playerid][VPN]);
            JsonGetBool(privacy, "proxy", User[playerid][Proxy]);

            JsonGetString(node, "city", User[playerid][City]);
            JsonGetString(node, "country", User[playerid][Country]);
            JsonGetString(node, "timezone", User[playerid][TimeZone]);
            JsonGetString(node, "postal", User[playerid][Zip]);
            
            JsonGetObject(node, "asn", asn);
            JsonToggleGC(asn, false);
            JsonGetString(asn, "name", User[playerid][Isp]);

            JsonGetObject(node, "company", org);
            JsonToggleGC(org, false);
            JsonGetString(org, "name", User[playerid][Org]);
        
            printf("vpn %s", User[playerid][VPN] ? "true" : "false");
            printf("proxy %s", User[playerid][Proxy] ? "true" : "false");

            printf("city %s", User[playerid][City]);
            printf("country %s", User[playerid][Country]);
            printf("timezone %s", User[playerid][TimeZone]);
            printf("postal %s", User[playerid][Zip]);
            printf("isp %s", User[playerid][Isp]);
            printf("org %s", User[playerid][Org]);

            JsonToggleGC(privacy, true);
            JsonToggleGC(asn, true);
            JsonToggleGC(org, true);
            JsonToggleGC(node, true);
        }
        default: printf("%s", E_HTTP:STATUS:status);
    }
    return 1;
}

Please give this a test and tell me if this works for you?

@ADRFranklin ADRFranklin reopened this May 3, 2023
@rettdev
Copy link

rettdev commented May 4, 2023

function OnGetJsonWeather(Request:id, E_HTTP_STATUS:status, Node:node){
    switch(status){
        case HTTP_STATUS_OK:{
            new Node:MainNode = node;
            //----------------------- MAIN ---------------------------------//
            JsonGetObject(MainNode, "main", g_Weather[nodeTemp]);
            JsonToggleGC(g_Weather[nodeTemp], false);
            JsonGetFloat(g_Weather[nodeTemp], "temp", g_Weather[TempWeather]);
            JsonGetFloat(g_Weather[nodeTemp], "temp_min", g_Weather[TempWeatherMin]);
            JsonGetFloat(g_Weather[nodeTemp], "temp_max", g_Weather[TempWeatherMax]);

            //----------------------- SYS AND NAME ---------------------------------//
            JsonGetObject(MainNode, "sys", g_Weather[nodeSys]);
            JsonToggleGC(g_Weather[nodeSys], false);
            JsonGetString(g_Weather[nodeSys], "country", g_Weather[CountryWeather]);
            JsonGetString(node, "name", g_Weather[CityName]);

            //----------------------- WEATHER ---------------------------------//
            JsonGetObject(MainNode, "weather", g_Weather[nodeWeather]);
            JsonToggleGC(g_Weather[nodeWeather], false);
            JsonArrayLength(g_Weather[nodeWeather], g_Weather[WeatherLength]);
            
            for(new i = 0; i < g_Weather[WeatherLength]; i++) {
                new Node:weatherNode;
                JsonArrayObject(g_Weather[nodeWeather], i, weatherNode);
                JsonGetString(weatherNode, "main", g_Weather[WeatherName]);
            }

            JsonToggleGC(g_Weather[nodeTemp], true);
            JsonToggleGC(g_Weather[nodeSys], true);
            JsonToggleGC(g_Weather[nodeWeather], true);

            printf("weather=%s, temp=%.0f°C, temp_min=%.0f°C, temp_max=%.0f°C, cityname=%s, country=%s", g_Weather[WeatherName], g_Weather[TempWeather], g_Weather[TempWeatherMin], g_Weather[TempWeatherMax], g_Weather[CityName], g_Weather[CountryWeather]);
        }
        default: printf("ERROR: OUTPUT %i", Float:status);
    }
    return 1;
}

output:
weather=, temp=285°C, temp_min=283°C, temp_max=288°C, cityname=, country=

@ADRFranklin
Copy link
Collaborator

@The-Rett Show the json output so it can be tested to see what is happening.

@rettdev
Copy link

rettdev commented May 4, 2023

`
printf("status=%i, weather=%s, temp=%.0f°C, temp_min=%.0f°C, temp_max=%.0f°C, cityname=%s, country=%s", _:status, g_Weather[WeatherName], g_Weather[TempWeather], g_Weather[TempWeatherMin], g_Weather[TempWeatherMax], g_Weather[CityName], g_Weather[CountryWeather]);

status=200, weather=, temp=285°C, temp_min=283°C, temp_max=288°C, cityname=, country=
`

@pushline
Copy link
Author

pushline commented May 5, 2023

@ADRFranklin I changed my API for the IP thing cause I couldn't retrieve the proxy/vpn parts cause they need to be paid.
It worked normally with custom IPs, thanks.

@The-Rett as I saw in Franklin's example u need to aswell desactivate the MainNode before everything and after the child ones.

@rettdev
Copy link

rettdev commented May 5, 2023

Funcionou, obrigado.

@pushline
Copy link
Author

pushline commented May 5, 2023

All good @ADRFranklin, he just forgot to deactivate the MainNode, thanks, it worked!

@pushline pushline closed this as completed May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants