JSON Post unexpected result in node.js
I am sending an HTTP request to a website (from node.js) that returns a JSON object. I am getting the expected JSON file. However, when I parse the JSON text, my program can't do anything.
var URL = 'http://www.omdbapi.com/?t=' + movie + '&y=&plot=short&r=json';
requestify.get(URL).then(function (response) {
console.log(response.getBody()); // It prints correctly
var jsonBody = response.getBody();
var jsonObject = JSON.parse(jsonBody);
if (jsonObject.Response == 'False') {
console.log('False'); //not printed
} else {
console.log('true'); //Not printed
}
});
Sample JSON output:
{"Response":"False","Error":"Movie not found!"}
+3
source to share
1 answer
response.body
is a text response. response.getBody()
should already be returning a parsed JSON response if you have the correct header content-type
.
Sending a JS object to JSON.parse
results in SyntaxError
.
+4
source to share