2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

Using response headers

This commit is contained in:
Matt Zabriskie
2014-08-27 03:12:29 -06:00
parent 514e281a1b
commit c0a9184739
7 changed files with 391 additions and 157 deletions
+38
View File
@@ -0,0 +1,38 @@
'use strict';
var forEach = require('./forEach');
function trim(str) {
return str.replace(/^\s*/, '').replace(/\s*$/, '');
}
/**
* Parse headers into an object
*
* ```
* Date: Wed, 27 Aug 2014 08:58:49 GMT
* Content-Type: application/json
* Connection: keep-alive
* Transfer-Encoding: chunked
* ```
*
* @param {String} headers Headers needing to be parsed
* @returns {Object} Headers parsed into an object
*/
module.exports = function parseHeaders(headers) {
var parsed = {}, key, val, i;
if (!headers) return parsed;
forEach(headers.split('\n'), function(line) {
i = line.indexOf(':');
key = trim(line.substr(0, i)).toLowerCase();
val = trim(line.substr(i + 1));
if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
});
return parsed;
};