Search code examples
node.jspostmanlinkedin-api

linkedin voyager api : Invalid response when calling API from code


I am using this linkedin-private-api library to connect to linkedin voyager api.

when I run this code I get an invalid json response something like this : �/J�>�2:��������i�f{�|tV4���>X��+��0

but when I use Postman I get a valid json response


    "data": {
        "metadata": {
            "type": "TypeaheadFindByType",
            "id": "ff39d918-5e52-4a14-9aa3-6c37c72cb81b",
            "$type": "com.linkedin.voyager.typeahead.TypeaheadMetadata"
        },
        "entityUrn": "urn:li:collectionResponse:IFrGi77656cS3A4SqIFwTMfpqxVjguPOPn6DHgNnpiQ=",
        "elements": [
            {
                "targetUrn": "urn:li:fs_geo:103688978",
                "text": {
                    "text": "Sousse Governorate, Tunisia",
                    "$type": "com.linkedin.pemberly.text.AttributedText"
                },
                "dashTargetUrn": "urn:li:fsd_geo:103688978",
                "type": "GEO",
                "trackingId": "JLoPDNEmRo2BEEQr26vinw==",
                "$type": "com.linkedin.voyager.typeahead.TypeaheadHitV2"
            }
        }

this is the full nodejs testing code:

const {Client} = require('linkedin-private-api');
const https = require('https')

test();

async function test() {

const client = new Client();

await client.login.userPass({
    username: "username",
    password: "password"
});

var _headers = client.request.request.defaults.headers

const options = {
  hostname: 'www.linkedin.com',
  path: '/voyager/api/typeahead/hitsV2?keywords=USA&origin=OTHER&q=type&queryContext=List(geoVersion-%3E3,bingGeoSubTypeFilters-%3EMARKET_AREA%7CCOUNTRY_REGION%7CADMIN_DIVISION_1%7CCITY)&type=GEO',
  method: 'GET',
  headers: _headers
}


https.get(options, function (res) {
    var json = '';
    res.on('data', function (chunk) {
        json += chunk;
    });
    res.on('end', function () {
        if (res.statusCode === 200) {
            try {
                // data is available here:
                console.log(json);
            } catch (e) {
                console.log('Error parsing JSON!');
            }
        } else {
            console.log('Status:', res.statusCode);
        }
    });
}).on('error', function (err) {
      console.log('Error:', err);
});

}


Solution

  • the other endpoint used a gzip encryption. but this endpoint did not. so I just set the accept-encoding to empty _headers["accept-encoding"] = "". It was confusing that even with the wrong encoding postman showed a correct response.