Forum Discussion
I tried and a type mismatch happens. url can't be assigned to string. url_getUser isn't defined anywhere else.
<#assign url_getUser = baseUrl + "users/query=user@domain.com" ?url/>
You have a space between the end quote (") and the question mark (?). Try removing the space. Also make sure baseUrl is a string. One other thing is to make sure only the query parameters are encoded. The following worked for me:
<#assign baseUrl = "http://test.test.com/"/>
<#assign url_getUser = baseUrl + "users?" + "query=user@domain.com"?url/>
${url_getUser}
Hope this helps,
Yuri
- perk10 years agoAdept
Hey thanks for the suggestion.
I tried:
<#assign base = "https://our_domain.zendesk.com/api/v2/"/>
<#assign url_getUser = base + "users?" + "query=username@company.com"?url/>
<#assign response_user = http.client.request(url_getUser).header("Authorization", "Bearer "+ response.content.access_token).json().get() />and still received the same error:
FreeMarker template error
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@13d041cd". See cause exception. The failing instruction (FTL stack trace): ---------- ==> #assign response_user = http.client.r... [in template "retrieve-org-zendesk.ftl" at line 29, column 25]
Totally at wits ends with what is going on in Lithium...
- YuriK10 years agoKhoros Expert
Can you paste a sample response that you get from the API call here? I'd like to check if JSON parsing is failing here. Also, please check the Content-Type header of the API response is returned as "application/json".
You can also try to remove the .json() and print out the response string to see if you are at least able to get a response at all.
- perk10 years agoAdept
Heyt YuriK,
The sample JSON response would be: {"users":[],"next_page":null,"previous_page":null,"count":0} if you are do ?query=abc
How should I print out the response when I remove json() ? There is no console in Lithium for output and I am relatively new with freemarker.
The endpoint sets the right content-type:
Content-Type: application/json; charset=UTF-8
Thanks for your help!
- YuriK10 years agoKhoros ExpertIf 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.
- perk10 years agoAdept
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!
- YuriK10 years agoKhoros Expert
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
- perk10 years agoAdept
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
- perk10 years agoAdept
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() />
- YuriK10 years agoKhoros ExpertGlad you were able to get to a solution! Sounds like Zendesk was expecting that header to be there.
Thank you,
Yuri
Related Content
- 5 years ago