Search code examples
xmlapilinkedin-api

LinkedIn API Doesn't Support All Characters?


I want to create a share with the LinkedIn API, but it won't allow me to use < or > for obvious reasons. Since they use XML it's quite tricky. I've tried url encoding, backslash insertion, and even HTML character replacement for < > like gt; and lt;

var body = "\
    <share>\
      <comment>" + message + " </comment>\
      <visibility><code>anyone</code></visibility>\
    </share>\
  ";

How can I include a < or > without having the LinkedIn server freak out when it sees the "unexpected character"?


Solution

  • Trying to construct XML by concatenating strings together is always going to be error-prone. You've not said in the question what programming language you're using, but any modern language should have libraries to build XML properly and handle all the necessary escaping for you.

    Alternatively this document suggests that the LinkedIn APIs can work with JSON as well as with XML, which may be an easier approach particularly if you're working in a language like JavaScript.