Help InternetOpenURL Not able to parse the response

When try to call below method in Amibroker v6.43.1 64-bit running in Windows 10 Prof version, I get junk characters as response. How do I read/decode these characters?

function getOrders(oAuthCode) {
		_TRACE("getOrders(): Start");
		bURL = "https://api.kite.trade/orders";
		headers = 
				  "accept: application/json, text/plain, */*\r\n" +
				  "accept-encoding: gzip, deflate, br\r\n" +
				  "authorization: "+oAuthCode+"\r\n"; // ALL headers in one string

		InternetSetHeaders( headers ); // call once 
		ih = InternetOpenURL( bURL);
		if( ih )
		{
			while( ( str = InternetReadString( ih ) ) != "" )
			{
			   _TRACE(str);
			}
			_TRACE("getOrders(): Step4");
			InternetClose( ih );
		}else{
			_TRACE("getOrders(): Step5: ih is a invalid handle");
		}
		_TRACE("getOrders(): Done");
	}

Below is the response
image

If I run the same in PostMan, Below is the response.

Below is the copy/paste of junk characters.

Ã0 Ы˜?›À{†îÝm¡Š’XFR !ôî%t¼ ‡£@ÌԐ±‹;½Îj&©ÑXê*gKj©³¸×ÐUz›ñ¢ ”~l[þ75Îqϛ̖‹v|

You have this:

"accept-encoding: gzip, deflate, br\r\n" 

in your code, so you got what you asked for. You asked for compressed (not plain text) data, so you got them. It is not enough to request compressed data. You have to DECOMPRESS them on your end. Otherwise you get binary compressed stream.

Always check the manual. It shows clearly how you should properly use functions. It even has an example how to handle decoding of compressed data.

You must use

INTERNET_OPTION_HTTP_DECODING = 65;

InternetSetOption( INTERNET_OPTION_HTTP_DECODING, 1 );

A must read:
https://www.amibroker.com/guide/afl/internetsetoption.html

1 Like

Thanks @Tomaze.

Even after updating the code as you have suggested, I am getting the same response (compressed).

function getOrders(oAuthCode) {
		_TRACE("getOrders(): Start");
		bURL = "https://api.kite.trade/orders";
		headers = "accept: application/json, text/plain, */*\r\n" +
				  "accept-encoding: gzip, deflate, br\r\n" +
				  "authorization: "+oAuthCode+"\r\n"; // ALL headers in one string
		
		
		INTERNET_OPTION_HTTP_DECODING = 65;
		InternetSetOption( INTERNET_OPTION_HTTP_DECODING, 1 );
		InternetSetHeaders( headers ); // call once 
		_TRACE("getOrders(): headers = "+headers);
		ih = InternetOpenURL( bURL);
		if( ih )
		{
			while( ( str = InternetReadString( ih ) ) != "" )
			{
			   _TRACE(str);
			}
			_TRACE("getOrders(): Step4");
			InternetClose( ih );
		}else{
			_TRACE("getOrders(): Step5: ih is a invalid handle");
		}
		_TRACE("getOrders(): Done");
	}

I have also tried removing "accept-encoding: gzip, deflate", getting the same compressed response.

    function getOrders(oAuthCode) {
		_TRACE("getOrders(): Start");
		bURL = "https://api.kite.trade/orders";
		headers = "accept: application/json, text/plain, */*,  \r\n" +
				  "authorization: "+oAuthCode+"\r\n"; // ALL headers in one string
		
		
		//INTERNET_OPTION_HTTP_DECODING = 65;
		//InternetSetOption( INTERNET_OPTION_HTTP_DECODING, 1 );
		InternetSetHeaders( headers ); // call once 
		_TRACE("getOrders(): headers = "+headers);
		ih = InternetOpenURL( bURL);
		if( ih )
		{
			while( ( str = InternetReadString( ih ) ) != "" )
			{
			   _TRACE(str);
			}
			_TRACE("getOrders(): Step4");
			InternetClose( ih );
		}else{
			_TRACE("getOrders(): Step5: ih is a invalid handle");
		}
		_TRACE("getOrders(): Done");
	}

Below is the response.

Copy the code from the USERS Guide , not yours. Your header is wrong.

Should be as in the users guide.

InternetSetHeaders("Accept-Encoding: gzip, deflate");

And use latest version, 6.43 is not the latest.

HI @Tomasz, Let me try with AmiBroker to 6.49.1.

Meanwhile, I have also tried with "Accept-Encoding: gzip, deflate" but getting the same compressed response.

Hi @Tomasz , I was able to get the uncompressed response with upgraded AmiBroker version 6.49.1. Thanks.

image

Hi @Tomasz,
I was trying to parse the json string which I had got it from Rest call but I dont know how to access specific attribute.

Below is the Json string which I got it from rest call and if I want to get the "sell_price" if the tradingsymbol="BANKNIFTY23N0842800CE"

{
    "status": "success",
    "data": {
        "net": [
            {
                "tradingsymbol": "BANKNIFTY23N0842800CE",
                "exchange": "NFO",
                "instrument_token": 10420482,
                "product": "NRML",
                "quantity": 375,
                "overnight_quantity": -375,
                "multiplier": 1,
                "average_price": 310,
                "close_price": 306.3,
                "last_price": 490,
                "value": 116250,
                "pnl": 67500,
                "m2m": 68887.5,
                "unrealised": 67500,
                "realised": 0,
                "buy_quantity": 0,
                "buy_price": 0,
                "buy_value": 0,
                "buy_m2m": 0,
                "sell_quantity": 375,
                "sell_price": 310,
                "sell_value": 116250,
                "sell_m2m": 114862.5,
                "day_buy_quantity": 0,
                "day_buy_price": 0,
                "day_buy_value": 0,
                "day_sell_quantity": 0,
                "day_sell_price": 0,
                "day_sell_value": 0
            },
            {
                "tradingsymbol": "BANKNIFTY23N0842800PE",
                "exchange": "NFO",
                "instrument_token": 10420738,
                "product": "NRML",
                "quantity": 375,
                "overnight_quantity": 375,
                "multiplier": 1,
                "average_price": 322.344,
                "close_price": 331.15,
                "last_price": 132.3,
                "value": -120879,
                "pnl": -71266.5,
                "m2m": -74568.74999999997,
                "unrealised": 71266.5,
                "realised": 0,
                "buy_quantity": 375,
                "buy_price": 322.344,
                "buy_value": 120879,
                "buy_m2m": 124181.24999999999,
                "sell_quantity": 0,
                "sell_price": 0,
                "sell_value": 0,
                "sell_m2m": 0,
                "day_buy_quantity": 0,
                "day_buy_price": 0,
                "day_buy_value": 0,
                "day_sell_quantity": 0,
                "day_sell_price": 0,
                "day_sell_value": 0
            },
            {
                "tradingsymbol": "NIFTY23N0219050CE",
                "exchange": "NFO",
                "instrument_token": 12820994,
                "product": "NRML",
                "quantity": 0,
                "overnight_quantity": 0,
                "multiplier": 1,
                "average_price": 0,
                "close_price": 0,
                "last_price": 83.25,
                "value": 1335,
                "pnl": 1335,
                "m2m": 1335,
                "unrealised": 1335,
                "realised": 0,
                "buy_quantity": 50,
                "buy_price": 83.2,
                "buy_value": 4160,
                "buy_m2m": 4160,
                "sell_quantity": 50,
                "sell_price": 109.9,
                "sell_value": 5495,
                "sell_m2m": 5495,
                "day_buy_quantity": 50,
                "day_buy_price": 83.2,
                "day_buy_value": 4160,
                "day_sell_quantity": 50,
                "day_sell_price": 109.9,
                "day_sell_value": 5495
            },
            {
                "tradingsymbol": "NIFTY23N0219250PE",
                "exchange": "NFO",
                "instrument_token": 12823298,
                "product": "NRML",
                "quantity": 0,
                "overnight_quantity": 0,
                "multiplier": 1,
                "average_price": 0,
                "close_price": 0,
                "last_price": 116.8,
                "value": -507.5,
                "pnl": -507.5,
                "m2m": -507.5,
                "unrealised": -507.5,
                "realised": 0,
                "buy_quantity": 50,
                "buy_price": 111.2,
                "buy_value": 5560,
                "buy_m2m": 5560,
                "sell_quantity": 50,
                "sell_price": 101.05,
                "sell_value": 5052.5,
                "sell_m2m": 5052.5,
                "day_buy_quantity": 50,
                "day_buy_price": 111.2,
                "day_buy_value": 5560,
                "day_sell_quantity": 50,
                "day_sell_price": 101.05,
                "day_sell_value": 5052.5
            }
        ],
        "day": [
            {
                "tradingsymbol": "NIFTY23N0219050CE",
                "exchange": "NFO",
                "instrument_token": 12820994,
                "product": "NRML",
                "quantity": 0,
                "overnight_quantity": 0,
                "multiplier": 1,
                "average_price": 0,
                "close_price": 0,
                "last_price": 83.25,
                "value": 1335,
                "pnl": 1335,
                "m2m": 1335,
                "unrealised": 1335,
                "realised": 0,
                "buy_quantity": 50,
                "buy_price": 83.2,
                "buy_value": 4160,
                "buy_m2m": 4160,
                "sell_quantity": 50,
                "sell_price": 109.9,
                "sell_value": 5495,
                "sell_m2m": 5495,
                "day_buy_quantity": 50,
                "day_buy_price": 83.2,
                "day_buy_value": 4160,
                "day_sell_quantity": 50,
                "day_sell_price": 109.9,
                "day_sell_value": 5495
            },
            {
                "tradingsymbol": "NIFTY23N0219250PE",
                "exchange": "NFO",
                "instrument_token": 12823298,
                "product": "NRML",
                "quantity": 0,
                "overnight_quantity": 0,
                "multiplier": 1,
                "average_price": 0,
                "close_price": 0,
                "last_price": 116.8,
                "value": -507.5,
                "pnl": -507.5,
                "m2m": -507.5,
                "unrealised": -507.5,
                "realised": 0,
                "buy_quantity": 50,
                "buy_price": 111.2,
                "buy_value": 5560,
                "buy_m2m": 5560,
                "sell_quantity": 50,
                "sell_price": 101.05,
                "sell_value": 5052.5,
                "sell_m2m": 5052.5,
                "day_buy_quantity": 50,
                "day_buy_price": 111.2,
                "day_buy_value": 5560,
                "day_sell_quantity": 50,
                "day_sell_price": 101.05,
                "day_sell_value": 5052.5
            }
        ]
    }
}

Below is one way I tried to get the tradingsymbol but no luck.

EnableScript("jscript");
<%
	  function _ParsePosition( json_string)
	  {
		var result;
		try {
			result = JSON.parse( json_string );
			result = item in result.data.net[0].tradingsymbol;
		} catch (e) {
			result=JSON.parse( "{\"status\": \"error\"}" );
		}
		return result
	  }
	%>

	function ParsePositionJSON( json_string ) {
		json = "";
		if(json_string != ""){
			script = GetScriptObject();
			json = script._Parse( json_string );
		}
		return json;
	}

Below is the code in AFL to get the "status" which is working but when I try to get "data" and next levels, I could not.

EnableScript("jscript");
<%
	  function _ParsePosition( json_string)
	  {
		var result;
		try {
			result = JSON.parse( json_string );
		} catch (e) {
			result=JSON.parse( "{\"status\": \"error\"}" );
		}
		return result
	  }
	%>

	function ParsePositionJSON( json_string ) {
		json = "";
		if(json_string != ""){
			script = GetScriptObject();
			json = script._Parse( json_string );
		}
		return json;
	}
       jsonObj = ParsePositionJSON(jsonStr);
	_TRACE("tradingsymbol = " + jsonObj.status);//this is working
	_TRACE("tradingsymbol = " + jsonObj.status.data.net[0].tradingsymbol);//not working

Just for Reference: If I do the same in JavaScript, below is the output (in Chrome console).

can I do it in AFL?

You have to do this (accessing attributes) INSIDE embedded JScript part (inside <% .... %>).
Inside those markers you have just functional Javascript so just use that.