Forum Discussion

perk's avatar
perk
Adept
10 years ago

TemplateModelException during JSON GET

I have the following code:

<#assign url_getUser = "https://DOMAIN.zendesk.com/api/v2/users?query=USER@COMPANY.com" />

<#assign response_user = http.client.request(url_getUser).header("Authorization", "Bearer "+ TOKEN).json().get() />

However, I keep getting the following error from the 2nd line:

Method "public final lithium.coreapi.webui.template.models.HttpClientResponseTemplateModel lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel.get() throws freemarker.template.TemplateModelException" threw an exception when invoked on lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel object "lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel@6c4c48f".

If I change it to 

<#assign url_getUser = baseUrl + "users/"+ "ID" + ".json" />

It works fine. I am able to make the first call in call but it doesn't work in freemarker. Any ideas why? 

16 Replies

  • YuriK's avatar
    YuriK
    Khoros Expert
    10 years ago
    If you remove .json() the response will simply be treated as a string. You should then be able to do ${response_user} to output this string.
  • perk's avatar
    perk
    Adept
    10 years ago

    Hm, this code is written as an endpoint. A custom component was written for us to process the data generated by the endpoint.

    How can I make the same request in the component?

     

    I tried adding this to the component:

     

    <#assign base = "https://domain.zendesk.com/api/v2/"/>
    <#assign url_getUser = base + "users?" + "query=email@domain.com" />
    <#assign response_user_no_json = http.client.request(url_getUser).header("Authorization", "Bearer "+ token).get() />
    ${response_user_no_json}

    and receive

     

    FreeMarker template error

    get(client) failed on instance of lithium.coreapi.webui.template.models.HttpTemplateModel. See cause exception.
    
    The failing instruction (FTL stack trace):
    ----------
    ==> #assign response_user_no_json = http...  [in template "preview" at line 28, column 29]
    ----------

     

     

    If I try removing the client:

     

    <#assign response_user_no_json = http.request(url_getUser).header("Authorization", "Bearer "+ token).get() />

    I get :

     

    FreeMarker template error

    For "...(...)" callee: Expected a method, but this evaluated to an extended_hash+string (lithium.coreapi.webui.template.models.RequestTemplateModel wrapped into f.e.b.StringModel):
    ==> http.request  [in template "preview" at line 28, column 62]
    
    The failing instruction (FTL stack trace):
    ----------
    ==> #assign response_user_no_json = http...  [in template "preview" at line 28, column 29]
    ----------

    Any help you can provide here will be very beneficial! 

  • YuriK's avatar
    YuriK
    Khoros Expert
    10 years ago

    Hey perk,

     

    Sorry for the frustrating debugging. I'm able to reproduce the error in an endpoint as follows (will output ERROR with the error or SUCCESS with the content):

     

    <#assign base = "https://domain.zendesk.com/api/v2/"/>
    <#assign url_getUser = base + "users?" + "query=email@domain.com"?url('UTF-8') />
    <#assign response_user_no_json = http.client.request(url_getUser).get() />
    <#if response_user_no_json.hasError>
    ERROR: ${response_user_no_json.error.message}
    <#else>
    SUCCESS: ${response_user_no_json.content}
    </#if>

    In my case I am getting "ERROR: error making http request: Unsupported Media Type" which means that the zendesk service is rejecting the request for some reason.

     

    I think DougS will be able to help you debug this further, but you may also want to check with Zendesk to figure out under what scenarios they send this error back.

     

    Note: http.client only works in endpoints currently, but you can access endpoints directly via url by clicking the "View http Endpoint" button on the Endpoints screen

     

    Hope this helps,

     

    Yuri

  • perk's avatar
    perk
    Adept
    10 years ago

    Hi Yuri, thanks for your help. Are you adding the parameters Authorization: Bearer SOME_TOKEN_WE_HAVE to the request?

     

    Can we add mulitple headers into the request? E.g. adding a "Content-type ?" It is strange though we will need a content-type set for a get request.

     

    Interestingly the request works fine in curl

     

    curl "https://domain.zendesk.com/api/v2/users?query=user@company.com" -H "Authorization: Bearer token" -v

     

    which translates to:

     

    > GET /api/v2/users?query=user@company.com HTTP/1.1

    > User-Agent: curl/7.37.1

    > Host: domain.zendesk.com

    > Accept: */*

    > Authorization: Bearer TOKEN

  • perk's avatar
    perk
    Adept
    10 years ago

    I added content type and I am getting success:

     

    <#assign response_user_no_json = http.client.request(url_getUser).header("Authorization", "Bearer "+ response.content.access_token).header("Content-type", "application/json").json().get() />

     

  • YuriK's avatar
    YuriK
    Khoros Expert
    10 years ago
    Glad you were able to get to a solution! Sounds like Zendesk was expecting that header to be there.

    Thank you,

    Yuri