Created
June 10, 2014 11:55
-
-
Save JohanObrink/164c205601d71bb71eb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
httpSignature = require('http-signature'), | |
crypto = require('crypto'); | |
var data = JSON.stringify({ foo: 'bar' }); | |
var md5 = crypto.createHash('md5'); | |
md5.update(data); | |
var options = { | |
host: 'localhost', | |
port: 3000, | |
path: '/samples/123/items', | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json', | |
'content-md5': md5.digest('base64') | |
} | |
}; | |
var req = http.request(options, function (res) { | |
console.log('STATUS: ' + res.statusCode); | |
console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
res.on('end', function () { | |
process.exit(1); | |
}); | |
}).on('error', function (err) { | |
console.error(err); | |
process.exit(1); | |
}); | |
var signOptions = { | |
keyId: 'someKey', | |
algorithm: 'hmac-sha256', | |
key: 'someSecret', | |
headers: ['request-line', 'date', 'content-type', 'content-md5'] | |
}; | |
try { | |
httpSignature.sign(req, signOptions); | |
} catch(err) { | |
console.error(err); | |
process.exit(1); | |
} | |
req.write(data) | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment