I want to send a POST request and try to process the output, more specifically, parse the JSON string that is inside a (well, the only) <pre>
element.
This is what I came up with:
var request = require('request');
// Set the headers
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
}
// Configure the request
var options = {
url: url,
method: 'POST',
headers: headers,
form: {'param1': 'value1', 'param2': 'value2'}
}
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
console.log("body: "+body)
var inside = body.match("<pre>(.*)</pre>");
console.log("inside: "+inside);
// what I really want:
var obj = JSON.parse(inside);
}
})
Here is the output:
body:
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
</head>
<body>
<div><pre>{
"variable": [
{
...
]
}
</pre></div>
</body>
</html>
inside: null
I get the body
as expected, but inside
is null. How can I read it correctly?
Also, I guess I need to sanitize those
s and "
s for JSON.parse()
to work. Although that might be another question, any help on this would also be much appreciated :)
Thanks,