Forum Discussion

jaikumar1's avatar
jaikumar1
Mentor
7 years ago

How to make REST API call

Hi,

 

I just looking into initiate the following REST API call using GET, PUT and DELETE method.

 

https://api.lithium.com/community/2.0/mytenantid/users/5?client-id=your-client-id

Could you please suggest the way to initiate the call. I have tried the rest() and restadmin() method. But it's not helpful, and also tried the ajax and that also not success. So guide me how to proceed on this?

 

8 Replies

  • jaikumar1's avatar
    jaikumar1
    Mentor
    7 years ago

    Thank you TariqHussain,

     

    I have tried the endpoint with necessary tenantid and customer id with the following code,

    <#assign responses=http.client.request("https","api.lithium.com","community/2.0/mytenantid/users/5").json().get()/>
    <#if responses.hasError>
      There is some error
    <#else>
      There is no error..
    </#if>

    But unfortunately, i'm getting 

    FreeMarker template error:
    The following has evaluated to null or missing:
    ==> responses.status  [in template "test_endpoint.ftl" at line 3, column 24]
    
    ----
    Tip: It's the step after the last dot that caused this error, not those before it.
    ----
    Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?

    Could you please guide me how to resolve this error?

     

     

  • TariqHussain's avatar
    TariqHussain
    Boss
    7 years ago

    jaikumar1 - Why do you want to use OAuth 2.0 process inside lithium, when you can get data using REST API v1 and REST API v2? 

     

    I believe you want to get the user data.  You can simply get it using REST API v2 e.g below. 

    SELECT * from users where id = '5'

    Using API v1. 

    /restapi/vc/users/id/5
  • jaikumar1's avatar
    jaikumar1
    Mentor
    7 years ago

    TariqHussain,

     

    Basically we just looking for Delete user API, so before i'm trying the user select with REST API, and based on the success i will try the delete user API.

  • TariqHussain's avatar
    TariqHussain
    Boss
    7 years ago

    jaikumar1 -  You did not add headers and authorization code for this.  Also, you can  ${responses.error.message} to get the error message. This will be helpful while debugging. 

     

     

    <#assign responses = http.client.request("https","api.stage.lithium.com","community/v1/<tenantID>/boards/id/<id>").header("client-id","xxxxxxxxxxxxxxxxxxxxxxxxxxxx").header("authorization", "Bearer xxxxxxxxxxxxxxxxxxxxxxxx").header("Content-Type", "application/json").get()/>
    
    <#if responses.hasError>
      There is some error
      ${responses.error.message}
    <#else>
      There is no error..
    </#if>

    For the authorization code, you need to follow Oauth 2.0. You can use postman to get it and directly pass it to the HTTP client.

     

    However, even after adding headers I was getting an issue API.

     http request not allowed: http client not allowed: hostname-lithium-domain-name 

    I have tried to add api.stage.lithium.com to Allowed HTTP client, however, lithium doesn't let me add this URL. You can try this at your end if you get any success. 

     

     

  • DougS's avatar
    DougS
    Khoros Oracle
    7 years ago

    Hi cblown!

     

    Our security team asked us to add this restriction in, because without it, it's possible (although unlikely) that an attacker could use http.client as an attack vector. This was before we started using api.lithium.com and it would be nice if we relaxed the restriction to allow this host name to be used.

     

    We have the rest freemarker object for making REST API calls to the community you are on. If you want to get a user via freemarker, you can make the following call:

     

    rest("2.0", "/users/5")

     

    We don't currently support doing REST V2 POST, PUT, or DELETE calls in freemarker. You don't want to in components, because they are meant for displaying page content only. It would be nice to add support for page initialization and endpoints to be able to make POST, PUT, or DELETE REST V2 calls, but we don't support this now. I believe you are able to create and edit users in Freemarker via REST V1 calls:

     

    There is no REST V1 call to close a user's account (delete), so there is not a way to delete a user via Freemarker (it would have to be done via HTTP).

     

    -Doug

     

    GavinD ChhamaJ YuriK

  • cblown's avatar
    cblown
    Boss
    7 years ago

    Thanks DougS - that makes sense, it would be helpful if access to api. was relaxed (or perhaps even available by request)

     

    Sorry to hijack this thread - but our pain point is Rating and Reviews, cause the V1 implementation isn't 100% (and seems slightly different to V2). However to be fair Ratings and Reviews normally run externally on product pages, so this restriction doesn't normally impact R&R. Currently R&R active cast widgets can actually "do more" than we're able to do via V1 and V2 API locally in Freemarker / via Endpoints yet using the V2 API externally is fine.