IN.API.Raw()


Raw

While the helper methods above provide you access to fundamental pieces of LinkedIn data, there are other resources in the LinkedIn API which don't yet have JS API helpers. Don't worry - the JavaScript API allows you to make these calls manually by exposing raw access to our REST endpoint. This includes any requests using a method other than GET, such as POST, PUT, and DELETE.

IN.API.Raw(url).method(methodType).body(bodyContent).result(resultCallback)
IN.API.Raw().url(url).method(methodType).body(bodyContent).result(resultCallback)
  • default url() - String 
    The API URL to invoke: should not include https://api.linkedin.com/v1.
  • method() - String 
    The HTTP method to use (GET, POST, PUT, or DELETE). If omitted, uses a default value of GET.
  • body() - String 
    For APIs with a POST body, POSTs and PUTs, this is the URL encoded post body. For GET requests, this does nothing.

For example, a People Search API call returning facet data for people living in the SF Bay Area:

IN.API.Raw("/people-search:(facets:(code,buckets:(code,name,count)))?facets=location&facet=location,us:84)")
      .result(function peopleSearchCallback(result) { /* code */ });

Retrieving Specific Data

Here's a short example showing how to use IN.API.Raw() to retrieve the full list of people who have liked a Network Update:

function displayNetworkUpdates(updates) {
  var profileDiv = document.getElementById("networkupdates");
     
  for (var i in updates.values) {
    var key = updates.values[i].updateKey; // unique key used to reference <p> in Raw API call results
    var share = updates.values[i].updateContent.person; // person making update
    profileDiv.innerHTML += "<p id='" + key + "'>" + share.firstName + " " + share.lastName
      + " shared " + share.currentShare.comment + ".</p>";

    IN.API.Raw("/people/~/network/updates/key=" + key + "/likes") // construct REST URL
      .result( function(value) { return function(likes) { // need to wrap inside function to get proper key
        document.getElementById(value).innerHTML += " (Liked by " + likes._total + " people)";
       } }(key))
      .error( function(error) { /* do nothing */ } );
  }
}


As above, there is code to loop through each Network Update and print out the name and comment. While a Network Update returns the first three people who have liked an update, it doesn't return them all. However, there's an API for that.

Reading the documentation shows that the URL is https://api.linkedin.com/v1/people/~/network/updates/key={KEY}/likes, where {KEY} is the updateKey field from the response. The IN.API.Raw() function assumes the leading http://api.linkedin.com/v1, so you only need to pass the end of the string.

As with the other IN.API functions, you can chain result() and error() methods to the end. Since these are all asynchronous calls, this example uses some closure-fu to capture the key, letting you easily append to the end of each update the information retrieved. This is a similar technique (without using closures) that we did when we assigned the member id to the paragraph tag at the top of the page. Here, for simplicity, is the total number of likers.

Using the POST, PUT, and DELETE Methods

The JavaScript API supports the full range of HTTP Methods, such as POST, PUT, and DELETE. Invoke these methods using the method() method, optionally passing a POST body string in the body() method.

Here's how to use them, using a member's current-status as an example. First, a GET request to read the current status:

IN.API.Raw("/people/~/current-status") // Read (GET) the status
  .result( function(result) { document.getElementById("statusDiv").innerHTML = result; } ) // XXX: Unsafe!
  .error(  function(error)  { /* do nothing */ } );

This uses IN.API.Raw() to retrieve the status and assigns it to statusDiv.

Be careful! Because a status can be any string, you must escape the results. Otherwise, you allow someone to inject HTML or JavaScript into your page. Dojo uses dojo.string.substitute() with a transform and jQuery uses .text().

To unset the status, use the same URI, but pass the DELETE method:

IN.API.Raw("/people/~/current-status") // Unset (DELETE) the status
  .method("DELETE")
  .result( function(result) { document.getElementById("statusDiv").innerHTML = "Status deleted"; } )
  .error(  function(error)  { /* do nothing */ } );

Many API calls that update content return either a 201 or 204 response code. These indicate the request was successful and either Created a new resource or has No Content in the response, respectively. In this case, we return 204 on success.

The JavaScript API funnels all 2xx responses to the result() callback function. However, you cannot determine which specific code was returned, nor can you access other HTTP Headers, such as a the Location header.

Last is the example to update a status. This uses the PUT method, to indicate that we're update the resource, along with JSON text in the body() with the new status.  See the Sending JSON data section of API Requests with JSON for how to format the contents of the data passed to the body() method.

IN.API.Raw("/people/~/current-status") // Update (PUT) the status
  .method("PUT")
  .body(JSON.stringify("Updating my status using the JavaScript API"))
  .result( function(result) { document.getElementById("statusDiv").innerHTML = "Status updated"; } )
  .error(  function(error)  { /* do nothing */ } );

The best way to convert a JavaScript value into JSON is with JSON.stringify(). Here is converts the JavaScript string into a JSON string. Mainly it adds quotation marks around the text. However, this also works for arrays and hashes, even when they are nested multiple levels. The function is also useful when you accept user input using a HTML form.