-1

Trying to do an API call to a provider of ours. I can use Postman and get the results I am expecting, but when I try to do it programmatically, it returns garbage.

Code (site and subscription key info has been scrubed):

            var url = @"https://xxx.xxx.xxx/api/v1/da_hrl_lmps?download=true&rowCount=40&startRow=1&datetime_beginning_ept=today&pnode_id=1338431025";

            using (WebClient client = new WebClient())
            {
                client.BaseAddress = "https://xxx.xxx.xxx/api/";
                client.Encoding = Encoding.UTF8;
                client.Headers.Add("Content-Type", "application/json");
                client.Headers.Add("Ocp-Apim-Subscription-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");

                string json = client.DownloadString(url);

                List<JsonReturnObject> responselist = JsonConvert.DeserializeObject<List<JsonReturnObject>>(json);

            }

What I am expecting to get in return is something like:

[
    {
        "datetime_beginning_utc": "2022-05-25T04:00:00",
        "datetime_beginning_ept": "2022-05-25T00:00:00",
        "pnode_id": 1338431025,
        "pnode_name": "PRST CE",
        "voltage": "345 KV",
        "equipment": "UN1 I",
        "type": "GEN",
        "zone": "COMED",
        "system_energy_price_da": 50.8,
        "total_lmp_da": 48.14,
        "congestion_price_da": -0.02,
        "marginal_loss_price_da": -2.64,
        "row_is_current": true,
        "version_nbr": 1
    },
    {
        "datetime_beginning_utc": "2022-05-25T05:00:00",
        "datetime_beginning_ept": "2022-05-25T01:00:00",
        "pnode_id": 1338431025,
        "pnode_name": "PRST CE",
        "voltage": "345 KV",
        "equipment": "UN1 I",
        "type": "GEN",
        "zone": "COMED",
        "system_energy_price_da": 49.33,
        "total_lmp_da": 7.33,
        "congestion_price_da": -40.03,
        "marginal_loss_price_da": -1.97,
        "row_is_current": true,
        "version_nbr": 1
    },
    {
        "datetime_beginning_utc": "2022-05-25T06:00:00",
        "datetime_beginning_ept": "2022-05-25T02:00:00",
        "pnode_id": 1338431025,
        "pnode_name": "PRST CE",
        "voltage": "345 KV",
        "equipment": "UN1 I",
        "type": "GEN",
        "zone": "COMED",
        "system_energy_price_da": 46.96,
        "total_lmp_da": 6.62,
        "congestion_price_da": -38.69,
        "marginal_loss_price_da": -1.65,
        "row_is_current": true,
        "version_nbr": 1
    }
]

instead, I am getting:

"\u001f�\b\0\0\0\0\0\u0004\0��]o�0\u0014\u0006�2�z;��{;&�\u0010\u0003��\u0006��k�*Қ�4\u0005\r�\u007f�d@T5]\\�Z�ԛ�N�>��\u001b\u007f�Y,�蝹/�����^��nQ�\n�8�bꊫ;&g�ѫ�<6�o���l�����/�e1C!�\u0014ȸ�7Z�מn|������\rM��<t�U?&��x�����]�Y���\r\u001fo��\u0015�u��~�˛[��������77/�r������oW�妭\u0016�\\\u038b�b`\u0019��t��a�y\u001a�\u0016P2��,\u0016M��ۮj꽛�\u00180����������l��S8�\u001f�6��j[.vm���v���w������%�_�'��8p�\u000e\\:\u0010�\u0010��ؔ�$p\u0011\0Gp&\u0001��\u0003���kp�\u0010\\��S�\u0082vAp�\u0012��8p�\u001f�\u0002ecW��`d\u0010\\a\u0002p\u001b\a.�\u0003�`�\u007f�p��+�%\0wq�*;p�4�h�\f�K>,�\tp���#�\u0003���\u001b0x\b��)�Iq5T�6��\u0004�\u0018'n�\u0013�\u0002\u001c�\u0016W8�\u0013L�\v�@�lj���%(\u0015/�A��\u001a�\t�E���O\\��#q\u0013�*\u0006TH<M/�qi\u0013�K���j=\u0014�T4BuܞP�y\u0002\U00078e09��M\u0012��Ĺ�\u0014���u����qy\u0013�˛FP�\u001dU\u0015\a���ӄ�q\u0012O�\u007f0.pb~��PX\u001f�*n�zF܄��P��b�K��_�4\u000eı��줸\u0004k��)2>�EN�/rZ*٣��U�;\u0014 �u�$X�<.sb~���j\u001deΰ�\u0006\u0013�UD����2'�9�\u0004=\u0016�\u0001qM\v�\u0005�]����2'�9�8r�\u0013\u0014�3gh�\u0014�A$\u0010�˜�_�$q\u001c�*��c�8��\u0013�\u0016��G�����2'����B;�\v�q\u0004y�:���,O\u0012�/s\u001a\u0003vt�\u0019\u0014�vx�=!~��S�\u001fZ�$�_��\u001ad|\u001d\u0017fX�ϊ3pg?�����'��955q�g�*��$���S��'Y_~\u0003\u0013�\u0014�\"\0\0"

Unsure why this is happening and any pointers would be appreciated.

Keith Clark
  • 597
  • 1
  • 5
  • 19
  • You probably need to [enable decompression](https://stackoverflow.com/questions/2973208/automatically-decompress-gzip-response-via-webclient-downloaddata). – Kirk Woll May 25 '22 at 18:17
  • Check if your JSON is compressed on in base32 or base64 (BSON) – Boyfinn May 25 '22 at 18:28
  • 2
    Check the request headers and response headers in Postman to see if there is anything about encoding/compression. Also use HttpClient instead of WebClient if possible. – tia May 25 '22 at 18:35

0 Answers0