Hi,
I use DF(2.1.1) to call a remote API and its working fine. But, if server response has a HTTP Code 400, DF encode the response.
Remote API send me this, with HTTP 400 and Content-Type: application/json
{
"errors": [
{
"errorMessage": "Device ID inválido"
}
]
}
And DF gives me this:
{
"status_code": 400,
"content": {
"error": {
"context": null,
"message": "{"errors":[{"errorMessage":"Device ID inv\\u00e1lido"}]}",
"code": 400
}
},
"content_type": null,
"format": 201
}
I can use lodash._.unescape to revert the htmlencode and JSON.parse to get a working json, but I still would not have the special chars converted, just one in this case “\u00e1”.
There is a way to get the original Remote API response?
Thanks!
Here’s an answer in return for your answer on REST API example
This just shows you the actual message, properly decoded.
var strServerResponse = "{"errors":[{"errorMessage":"Device ID inv\\u00e1lido"}]}";
function rhtmlspecialchars(str) {
// Remove HTML Special characters first
if (typeof(str) == "string") {
str = str.replace(/>/ig, ">");
str = str.replace(/</ig, "<");
str = str.replace(/'/g, "'");
str = str.replace(/"/ig, '"');
str = str.replace(/&/ig, '&'); /* must do & last */
}
return str;
}
// First remove the html special characters
var strServerResponseWOspecialChars = rhtmlspecialchars(strServerResponse);
// Use JSON parse to turn it into a JSON Object
var jsonObj = JSON.parse(strServerResponseWOspecialChars);
// Now easily pull out the stuff we need from this object
alert(jsonObj.errors[0].errorMessage); // assuming that the message is in this format "{'errors':[{'errorMessage':'Device ID inválido'}]}"
Cheers,
M.M
Thanks @m_menon,
I appreciate your help!
I’m using something like this now, but I dont want to do this all the time. The remote server response is ok when I use postman or curl.
Cheers
haha yah sometimes weird things do happen. Like I was struggling with setting the charset to utf-8 on bitnami / dreamfactory / apache but it simply doesn’t work. Have tried all possible things including almost every solution on SO but no luck…hahaha, sometimes silly problems can keep one awake for the whole night
Hope you get lucky with the response decoding thingy, the right way. I am not a big fan of hacks too. It’s short term fun and sometimes long term misery
Cheers,
M&M