javascript - How to do a HTTP GET in coffeescript to get a JSON from a REST API - Stack Overflow

I'm looking for a simple way to fetch a REST API in Coffeescript and get a json. I'm using th

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

I 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745308247a4621844.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信