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