Sanitizing invalid characters in the HTTP response header before importing into MongoDB in JSON format

When requesting HTTP responses using Node.js and importing them into MongoDB, I noticed that one or two URLs will contain headers containing invalid characters (as they are used by keys), which will lead to the failure of the whole script, since I try to import into MongoDB. The following is an example:

{ "url": "divensurf.com", "statusCode": 200, "headers": { "x-varnish": "2236710953 2236710300", "vary": "Accept-Encoding,Cookie,X-UA-Device", "cache-control": "max-age=7200, must-revalidate", "x-cache": "V1HIT 2", "content-type": "text/html; charset=UTF-8", "page.ly": "v4.0", "x-pingback": "http://divensurf.com/xmlrpc.php", "date": "Thu, 21 Mar 2013 19:40:59 GMT", "transfer-encoding": "chunked", "via": "1.1 varnish", "connection": "keep-alive", "last-modified": "Thu, 21 Mar 2013 19:40:57 GMT", "age": "2" } } 

The header / key "page.ly" will cause the script to crash because it contains an invalid character . . Are there ways to clear this key / header that is enclosed in a quote by removing these illegal characters before I import this document into MongoDB?

Below is the code in which I am requesting answers:

 (function (i){ http.get(options, function(res) { var obj = {}; obj.url = hostNames[i]; obj.statusCode = res.statusCode; obj.headers = res.headers; db.scrape.save(obj); // imports headers into MongoDB }).on('error',function(e){ console.log("Error: " + hostNames[i] + "\n" + e.stack); // prints error stack onto console }) })(i); 

For example, this would be from "page.ly" to "pagely"

EDIT: RESOLVES. Check Gaelโ€™s answer.

+4
source share
1 answer
 obj.headers={}; for(var item in res.headers){ obj.headers[ item.replace(/\./,'')] = res.headers[item]; } 
+1
source

All Articles