Skip to content

Instantly share code, notes, and snippets.

@iangreenleaf
Forked from smathy/web_helper.coffee
Created December 2, 2011 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iangreenleaf/1425218 to your computer and use it in GitHub Desktop.
Save iangreenleaf/1425218 to your computer and use it in GitHub Desktop.
https = require 'https'
module.exports.request = ( options, success_handler, error_handler) ->
req_data = if options.data? then JSON.stringify options.data else ""
options['headers'] ?= {}
options['headers']['Content-length'] = req_data.length
req = https.request options, (res) ->
data = ''
res.on 'data', (c) ->
data += c
res.on 'end', ->
if 200 <= res.statusCode < 300
if success_handler? and data?
success_handler JSON.parse data
else
if error_handler?
error_handler res
else
console.log "Error: #{res.statusCode}\n#{data}"
req.write(req_data)
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment