Forum Discussion

Akenefick's avatar
Akenefick
Genius
6 months ago

Trouble with http.client.response

I'm making the following http.client.request call in an endpoint.

<#assign response = http.client.request("https", "api.cognitive.microsofttranslator.com", "/translate?api-version=3.0&to=es&textType=html")
    .header("Ocp-Apim-Subscription-Key", "MyKeyGoesHere")
    .header("Ocp-Apim-Subscription-Region", "eastus")
    .header("Content-Type", "application/json")
    .body("[{'Text':'<p class=\"some-class-ak\">Hello, what is your name?</p>'}]", "application/json")
    .post() />

<#if response.hasError>
    ${response.error.message}
<#else>
    ${response.content}
</#if>

 

Testing in postman I got this response.

[
    {
        "detectedLanguage": {
            "language": "en",
            "score": 1.0
        },
        "translations": [
            {
                "text": "<p class=\"some-class-ak\">Hola, ¿cómo te llamas?</p>",
                "to": "es"
            }
        ]
    }
]

 

However, from the endpoint I see this with the quotes encoded and an error in the console. I'll copy both below the image.

[{&#34;detectedLanguage&#34;:{&#34;language&#34;:&#34;en&#34;,&#34;score&#34;:1.0},&#34;translations&#34;:[{&#34;text&#34;:&#34;<p class="\&#34;some-class-ak\">Hola, ¿cómo te llamas?</p>&#34;,&#34;to&#34;:&#34;es&#34;}]}]

json_document.js:1 SyntaxError: Expected property name or '}' in JSON at position 7 (line 2 column 7)
at JSON.parse (<anonymous>)

 

I've done similar things before and received recognizable JSON, so I'm not sure what I'm doing wrong. I have the endpoint set to apllication/json

 

Any help would be appreciated. Thanks!

 

  • Sounds like you're running into freemarker auto-escape. 

    https://freemarker.apache.org/docs/dgui_misc_autoescaping.html

    Basically you need to turn off autoescape or change outputformat to json. Also make sure your endpoint output format is set for application/json (this changes the content-type header in the endpoint response).

    You shouldn't need the ?replace after those changes.

  • MattV's avatar
    MattV
    Khoros Staff

    Sounds like you're running into freemarker auto-escape. 

    https://freemarker.apache.org/docs/dgui_misc_autoescaping.html

    Basically you need to turn off autoescape or change outputformat to json. Also make sure your endpoint output format is set for application/json (this changes the content-type header in the endpoint response).

    You shouldn't need the ?replace after those changes.

    • Akenefick's avatar
      Akenefick
      Genius

      Thanks! I finally got it working.

      I surrounded the whole thing with 

      <#noautoesc>
       
      </#noautoesc>
       
      I added .json() to my http.client.request
       
      I was still getting an error because it said it was expecting a string, but it was a sequence instead. I think it didn't like that Microsoft's entire response was an array wrapped in [square brackets] instead of {curly braces}. So, I had to do this 
       
      <#assign output = utils.json.toJson({"output":response.content})/>
       
      Finally, it worked! The escaping is gone, and the weird double quotes are gone. I'm finally getting usable Json.
       
      My final code.
       

       

      <#noautoesc>
      <#assign body = http.request.body />
      <#--  <#assign body = "[{'Text':'<p class=\"testing\">Hello, what is your name?</p>'}]" />  -->
      
      <#assign response = http.client.request("https", "api.cognitive.microsofttranslator.com", "/translate?api-version=3.0&to=es&textType=html")
          .header("Ocp-Apim-Subscription-Key", "My Key Here")
          .header("Ocp-Apim-Subscription-Region", "eastus")
          .header("Content-Type", "application/json")
          .body(body, "application/json")
          .json()
          .post() />
      
      <#if response.hasError>
          ${response.error.message}
      <#else>
          <#assign output = utils.json.toJson({"output":response.content})/>
          ${output}
      </#if>
      
      </#noautoesc>

       

       
  • I found this post Decoding a value using freemarker in a component o... - Atlas (khoros.com) with a similar issue and updated my code with this to replace the quotes.

     

     

    ${response.content?replace('&#34;','"')}

     

     
    I'm not crazy about that fix and I'm also questioning if it's even necessary.
     
    I found a bigger problem in the process. It was something different causing the error in the console. An extra quote seen below is breaking the Json and causing the error.
     

     

     Here it is from postman and after adding the "replace" code above which just makes it easier to see.
     
     

     

     So, I'm very confused about what is happening here. Anyone know what I'm missing?

    Thanks!

  • And here is one possible use for the endpoint above (after adding the option to pass it a language selection) 😃