0

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>{
&nbsp;&quot;variable&quot;: [
&nbsp;{
...
&nbsp;&nbsp;]
}
</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 &nbsp;s and &quot;s for JSON.parse() to work. Although that might be another question, any help on this would also be much appreciated :)

Thanks,

jeff
  • 13,055
  • 29
  • 78
  • 136
  • @jeff, are you using jquery or plain xhr request? because xhr has a property called `responseType = "document";`. If you set it, your response will return a`.responseXML` property with an already decoded document. Anyway, if you still wans to use regex, you'll have to decode your string (to remove the special chars) and then JSON parse it. – mrlew Jan 13 '17 at 15:59
  • @mrlew I'm doing this from Node.js. The duplicate suggestion solved the problem of parsing it. Thanks, – jeff Jan 13 '17 at 16:02

0 Answers0