I'm looking for a simple way to fetch a REST API in Coffeescript and get a json. I'm using the request library. When I do that, nothing happen, no error.
request = require 'request'
resp = ""
request.get {uri:'', json : true}, (err, r, body) -> resp = body
console.log "BODY: " + resp
What do I do wrong? Do you know a better way to get a json from a REST api in coffeescript? Thanks a lot!
I'm looking for a simple way to fetch a REST API in Coffeescript and get a json. I'm using the request library. When I do that, nothing happen, no error.
request = require 'request'
resp = ""
request.get {uri:'https://api.service.co/search?query=paris', json : true}, (err, r, body) -> resp = body
console.log "BODY: " + resp
What do I do wrong? Do you know a better way to get a json from a REST api in coffeescript? Thanks a lot!
Share Improve this question asked Jul 29, 2015 at 13:08 user420574user420574 1,4575 gold badges21 silver badges33 bronze badges1 Answer
Reset to default 4I suspect your issue is that request.get
executes asynchronously, so by the time it hits the console.log
statement, resp is always ""
.
Try this:
request = require 'request'
resp = ""
request.get {uri:'https://api.service.co/search?query=paris', json : true}, (err, r, body) ->
resp = body
console.log "BODY: " + resp