Working with REST apis

Hi erveryone,

We are currently in the process of rolling SDL WorldServer out in our organisation. We are running our tests on v. 11.0 but will probably go live with 11.1 as soon as it is available.

I am thinking of using the REST API to access some data. However, I am quite new to REST APIs in general. I am able to set up the connection to the server but it stops there. I am not sure how I should proceed to read the response and retrieve the sessionID. The response looks like formatted HTML and not really like JSON content.

The current documentation of the API does not provide samples on how to implement the API. I wanted to ask if some of you maybe have samples or code parts they would agree to share with me. it would be a great help. Code can be in c# or even javascript.

Thanks in advance to those who will reply.

Regards,

Laurent

Parents Reply Children
  • Hi Laurent,

    the API is (as you already know) REST based. This means that one should just be making HTTP calls with correct verbs (according to the documentation) no matter which tools / programming languages are used.

    The base URL for the REST API is: http(s)://{your-server-name}:{port-number}/ws-api/v1
    The endpoints are then added to to that base URL and one has to make sure that the http request has a Content-Type header set to "application/json".

    For example, to login you have to make a POST request to {BASE_URL}/login, set Content-Type to application/json and in the body of the request send following json:

    {
    "username":"your-username",
    "password":"your-password"
    }

    as a response you would get something similar to the following json:
    {
    "sessionId": "956580144",
    "expirationTime": "",
    "userDetails": {
    "fullName": "Admin",
    "fingerprint": "2b35927d-cfef-4884-80bd-9d73ca849965",
    "language": {
    "id": 1033,
    "languageCode": "en",
    "countryCode": "US",
    "locale": "en_US_English (United States)"
    },
    "regionalSettingsLocale": "en_US_English (United States)"
    },
    "loginOutcome": "LOGIN_SUCCEEDED",
    "lastUpdateTime": 1465461211916,
    "daysToPwdExpire": -1
    }

    What's important is the session id since this is used as session token for all the subsequent requests.

    For example, if I wanted to get a list of workflows configured on the server I would then make a GET request
    using the session id from previous request as token parameter:

    GET {BASE_URL}/workflows?token=956580144

    and this gets you back json that would look something like this:

    {
    "items": [
    {
    "id": 1002,
    "name": "Simple WF with Review",
    "description": "",
    "type": "ASSET",
    "steps": [
    {
    "id": 1011,
    "type": "START",
    "name": "Start",

    .... the rest json response left out
    }

    To make these REST calls you can use any REST Client / library you want (e.g. in C# that would be HttpClient from System.Net.Http namespace, have a look here: msdn.microsoft.com/.../system.net.http.httpclient(v=vs.110).aspx)

    Good example on how to call REST API from .NET, as Paul already mentioned, can be found in the GroupShareKit.NET
    (the code is freely available on GitHub: github.com/.../groupsharekit.net)

    Hope this was helpful.

    Thanks,
    Mio
  • Dear Mio, dear Andrea,

    thank you very much for the information and the quick reaction. I will have a deeper look into the information you sent. Thanks to Mio's message, I could already spot the mistake in my code and understand why I could see the session ID in the response: the "v1" was missing in by base URL... Maybe I oversaw the syntax of the base url in the documentation.

    Thanks for your help. I am sure this will give me a better understanding of the API.

    Regards,

    Laurent
  • This might be a little late, but I'll ask anyway...

    Miodrag, how did you figure out how these REST end-points work? So far I haven't found any useful information.

    If at all possible I'm looking for some information about:
    1. Is there a predefined order of the REST calls (as you imply)
    2. What do the seperate endpoints need for parameters? Query and/or URL parameters
    3. What do the end-points return (other than HTTP codes)?

    Thanks in advance!

    /Ted