最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论