Mario Perry
Mario Perry

Reputation: 83

{EASY} Rapid API returning value Dict error

I'm using rapid API to fetch LinkedIn data. This particular API, uses a simple HTTPS GET request and returns json. I'm writing this is Google App Script (Javascript) and return this error when running a test.

function callLinkedInAPI () {
  // https://rapidapi.com/iscraper/api/linkedin-profiles-and-company-data/ API website
  // https://iscraper.io/docs APi Docs

    var payload = {
    "profile_id": "williamhgates",
    "profile_type": "personal",
    "contact_info": false
    }

  var headers = {
    "contentType": "application/json",
        "x-rapidapi-host": "linkedin-profiles-and-company-data.p.rapidapi.com",
        "x-rapidapi-key": "76db3a1901mshd1518c9ce779bdep1c5920jsn3e0ec4853b66"
      }

  var url = `https://linkedin-profiles-and-company-data.p.rapidapi.com/api/v1/profile-details`

  var requestOptions = {
    'method': "POST",
    'headers': headers,
    'payload': payload,
    'muteHttpExceptions': true,
    'redirect': 'follow'
    };

  var response = UrlFetchApp.fetch(url, requestOptions);
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  console.log(data)

  }

I recieve this error upon calling the api

[ { msg: 'value is not a valid dict',
       loc: [Object],
       type: 'type_error.dict' } ]

Upvotes: 1

Views: 323

Answers (1)

NightEye
NightEye

Reputation: 11184

Use Content-Type instead of contentType in headers and pass the payload as string.

Content-Type:

var headers = {
  "Content-Type": "application/json",
  "x-rapidapi-host": "linkedin-profiles-and-company-data.p.rapidapi.com",
  "x-rapidapi-key": "76db3a1901mshd1518c9ce779bdep1c5920jsn3e0ec4853b66"
}

var url = `https://linkedin-profiles-and-company-data.p.rapidapi.com/api/v1/profile-details`

var requestOptions = {
  'method': "POST",
  'headers': headers,
  'payload': JSON.stringify(payload),
  'muteHttpExceptions': true,
  'redirect': 'follow'
};

Or move it as is under requestOptions (you still need to pass the payload as string)

contentType:

var headers = {
  "x-rapidapi-host": "linkedin-profiles-and-company-data.p.rapidapi.com",
  "x-rapidapi-key": "76db3a1901mshd1518c9ce779bdep1c5920jsn3e0ec4853b66"
}

var url = `https://linkedin-profiles-and-company-data.p.rapidapi.com/api/v1/profile-details`

var requestOptions = {
  'contentType':"application/json",
  'method': "POST",
  'headers': headers,
  'payload': JSON.stringify(payload),
  'muteHttpExceptions': true,
  'redirect': 'follow'
};

Output:

output

References:

Upvotes: 1

Related Questions