Node.JS Restify and post Events

Hello I have a post event that takes in key value and maps it to a sproc. This part works very well. However, for some reason I cannot return the data response as JSON.

It says the return type is "octet stream" even though I'm setting it to application/json.

I'm a little new to REST, so if I'm doing something incorrectly, please let me know.

server.post({ path: PATH + '/data.json', version: '1.0' }, genericData);


function genericData(req, res, next) {

    if (req.params.data != null){

        var sp_code_name = config.get('data:' + req.params.data);
        var connection = new sql.Connection(conn_str, function(err) {
            // ... error checks
                if(err) {
                        return console.log("Could not connect to sql: ", err);
                        connection.close();
                        }
                });

                var request = new sql.Request(connection);
                console.log(sp_code_name);

                request.execute(sp_code_name, function (err, recordset, returnValue) {

                    if (err) {
                        connection.close();
                        return console.log("Is this a good query or the right table?: ", err);

                    }

                    //if (recordset && returnValue == 0) {
                        res.setHeader('Access-Control-Allow-Origin','*');
                        res.setHeader('content-type', 'application/json');
                        res.send(200, recordset[0]);
                        console.log(recordset[0]);

                      //  return next();
                   // }

                   // return next();

                    connection.close();
                }); 


    }

    res.setHeader('content-type', 'application/text');
    res.send(200, "Something is wrong with the stream.");


}

You need to wrap your error response in an else clause:

if (req.params.data != null){
    ...
} else {
    res.setHeader('content-type', 'application/text');
    res.send(200, "Something is wrong with the stream.");
}

Without the else clause, it's getting called on every request before your request.execute callback is called. That's setting your content-type to application/text instead of application/json as you're expecting.