admin管理员组文章数量:1419611
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
本文标签: javascriptHow to do a HTTP GET in coffeescript to get a JSON from a REST APIStack Overflow
版权声明:本文标题:javascript - How to do a HTTP GET in coffeescript to get a JSON from a REST API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745308247a2652779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论