Forum Discussion

sachin's avatar
sachin
Mentor
10 years ago

Getting issue with http.client.request

Hi,

 

We are making call to third party API call to get JSON data. However when we read the variable it throws error. Below is the code:
--------------------
<#assign req = http.client.request("https://api.abc.com/v1/rest").json() />
<#assign res = req.call("GET") />
${res}
-----------------------

If we hit the API call on browser response looks like below:
[{"Id":"88310f87-ea6f-4fec-ab4d-389d8c127b18","Category":"Bug Reports","HTML":"\u003ciframe width=\"640\" height=\"590\" src=\"https://api.abc.com/v1/rest/88310f87-ea6f-4fec-ab4d-389d8c127b18\" frameborder=\"0\" allowfullscreen webkitallowfullscreen scrolling=\"no\"\u003e\u003c/iframe\u003e","Language":{"Id":2,"Name":"English","Locale":"English","Code":"en"},"Privacy":"Public","PublishDate":"8/4/2014 11:43:48 PM","SkillLevel":"Beginner","Status":"Published","Title":"Ripple Mobile Emulator and the \"New Video Play\" issue - Not Reproducible","TimelineHeight":190},{"Id":"69c12a54-8b3d-4627-a5b7-d1e1fb5ab179","Category":"Bug Reports","HTML":"\u003ciframe width=\"640\" height=\"590\" src=\"https://api.abc.com/v1/rest/69c12a54-8b3d-4627-a5b7-d1e1fb5ab179\" frameborder=\"0\" allowfullscreen webkitallowfullscreen scrolling=\"no\"\u003e\u003c/iframe\u003e","Language":{"Id":2,"Name":"English","Locale":"English","Code":"en"},"Privacy":"Public","PublishDate":"8/5/2014 12:25:37 AM","SkillLevel":"Beginner","Status":"Published","Title":"Ripple Mobile Emulator (iPhone) and the \"New Video Play\" issue - Not Reproducible","TimelineHeight":190}]

 

 

We are able to read the if we just read it as string but then we are seeing the character conversion to ASCII. Is there a way to convert them back.Below is the code and response:

--------------------
<#assign req = http.client.request("https://api.abc.com/v1/rest") />
<#assign res = req.call("GET") />
${res}
-----------------------

Respone:
[{&#34;Id&#34;:&#34;88310f87-ea6f-4fec-ab4d-389d8c127b18&#34;,&#34;Category&#34;:&#34;Bug Reports&#34;,&#34;HTML&#34;:&#34;\u003ciframe width&#61;\&#34;640\&#34; height&#61;\&#34;590\&#34; src&#61;\&#34;https://api.abc.com/v1/rest/88310f87-ea6f-4fec-ab4d-389d8c127b18\&#34; frameborder&#61;\&#34;0\&#34; allowfullscreen webkitallowfullscreen scrolling&#61;\&#34;no\&#34;\u003e\u003c/iframe\u003e&#34;,&#34;Language&#34;:{&#34;Id&#34;:2,&#34;Name&#34;:&#34;English&#34;,&#34;Locale&#34;:&#34;English&#34;,&#34;Code&#34;:&#34;en&#34;},&#34;Privacy&#34;:&#34;Public&#34;,&#34;PublishDate&#34;:&#34;8/4/2014 11:43:48 PM&#34;,&#34;SkillLevel&#34;:&#34;Beginner&#34;,&#34;Status&#34;:&#34;Published&#34;,&#34;Title&#34;:&#34;Ripple Mobile Emulator and the \&#34;New Video Play\&#34; issue - Not Reproducible&#34;,&#34;TimelineHeight&#34;:190},{&#34;Id&#34;:&#34;69c12a54-8b3d-4627-a5b7-d1e1fb5ab179&#34;,&#34;Category&#34;:&#34;Bug Reports&#34;,&#34;HTML&#34;:&#34;\u003ciframe width&#61;\&#34;640\&#34; height&#61;\&#34;590\&#34; src&#61;\&#34;https://api.abc.com/v1/rest/69c12a54-8b3d-4627-a5b7-d1e1fb5ab179\&#34; frameborder&#61;\&#34;0\&#34; allowfullscreen webkitallowfullscreen scrolling&#61;\&#34;no\&#34;\u003e\u003c/iframe\u003e&#34;,&#34;Language&#34;:{&#34;Id&#34;:2,&#34;Name&#34;:&#34;English&#34;,&#34;Locale&#34;:&#34;English&#34;,&#34;Code&#34;:&#34;en&#34;},&#34;Privacy&#34;:&#34;Public&#34;,&#34;PublishDate&#34;:&#34;8/5/2014 12:25:37 AM&#34;,&#34;SkillLevel&#34;:&#34;Beginner&#34;,&#34;Status&#34;:&#34;Published&#34;,&#34;Title&#34;:&#34;Ripple Mobile Emulator (iPhone) and the \&#34;New Video Play\&#34; issue - Not Reproducible&#34;,&#34;TimelineHeight&#34;:190}]


Appreciate the help here.

 

Thanks,
Sachin

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)

    Hi sachin 

     

    If you are expecting JSON, have you tried using (vary the HTTP request as appropriate for your need)

     

    <#assign resp = http.client.request("https", "api.test.com", "/context/path").body("param1=val1", "application/x-www-form-urlencoded").json().post() />
    <#if resp.hasError>
    Error!
    <#else>
    <#assign jsonResp = resp.content >
    ${resp.content}
    </#if> 
    

    Thanks,

    • sachin's avatar
      sachin
      Mentor

      Thanks PaoloT 

       

      I tried the solution but it did not helped. I am getting freemarker error. Below is the error.

      { "error":"
      FreeMarker template error:
      Method public final lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel.body(java.lang.String,java.lang.String) throws freemarker.template.TemplateModelException takes exactly 2 arguments, 1 was given.

      The failing instruction (FTL stack trace):
      ----------
      ==> #assign resp = http.client.request("h... [in template "parsing_poc_remove_later.ftl" at line 1, column 1]
      ----------

      Java stack trace (for programmers):
      ----------
      freemarker.template.TemplateModelException: [... Exception message was already printed; see it above ...]
      at freemarker.ext.beans.SimpleMemberModel.unwrapArguments(SimpleMemberModel.java:96)
      at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:107)
      at freemarker.core.MethodCall._eval(MethodCall.java:98)
      at freemarker.core.Expression.eval(Expression.java:111)
      at freemarker.core.Dot._eval(Dot.java:74)
      at freemarker.core.Expression.eval(Expression.java:111)

       

       

      Thanks,
      Sachin

      • PaoloT's avatar
        PaoloT
        Lithium Alumni (Retired)

        Hi sachin 

         

        you need to access the actual values from a real response - the above was just mock code which you need to adapt.

         

        Try this for a working example - remember to whitelist ip.jsontest.com in the Admin settings.

         

        <#assign resp = http.client.request("http", "ip.jsontest.com", "/").json().get() />
        <#if resp.hasError>
        Error!
        <#else>
        <#assign jsonResp = resp.content >
        ${jsonResp.ip}
        </#if> 

         

        Hope it helps