 
   req = http.request options, (res) ->
     res.setEncoding 'utf8'
-
-    # rabbit api sends a 204 for succesfull put and delete requests
-    if res.statusCode is 204
-      callback null, JSON.stringify {ok: "request successful"}
-    else
-      res.on "data", (chunk) ->
-        body = JSON.parse chunk
+    res.body = ''
+    res.on "data", (chunk)-> res.body += chunk
+    res.on "end", ->
+      # rabbit api sends a 204 for succesfull put and delete requests
+      if res.statusCode is 204
+        callback null, JSON.stringify {ok: "request successful"}
+      else
+        try
+          body = JSON.parse chunk
+        catch error
+          console.log error
+          body = {error: "response should be json."}
+        
         if body.error?
           callback body, null
 
-
-  req.on 'error', (e) ->
-    console.log e
-    callback e, null
+  req.on 'error', (error) ->
+    console.log error
+    callback error, null
 
   req.write JSON.stringify args
   req.end()

    host: mqConfig.host
    port: mqConfig.apiPort
    auth: mqConfig.login + ':' + mqConfig.password
    path: '/api/' + method
    method: 'GET'

  req = http.request options, (res) ->
    res.setEncoding 'utf8'
    res.on 'data', (chunk) ->
      callback null, chunk

  req.on 'error', (e) ->
    callback e, null

  req.end()

apiSet = (method, args, callback) ->


  req = http.request options, (res) ->
    res.setEncoding 'utf8'
    res.body = ''
    res.on "data", (chunk)-> res.body += chunk
    res.on "end", ->
      # rabbit api sends a 204 for succesfull put and delete requests
      if res.statusCode is 204
        callback null, JSON.stringify {ok: "request successful"}
      else
        try
          body = JSON.parse chunk
        catch error
          console.log error
          body = {error: "response should be json."}
        
        if body.error?
          callback body, null

  req.on 'error', (error) ->
    console.log error
    callback error, null

  req.write JSON.stringify args
  req.end()
