2

I am trying to get user data in my Rails app.

I am doing this:

  url_so = "http://api.stackoverflow.com/1.0/users/231917?type=jsontext"
  curl = Curl::Easy.new(url_so)
  curl.perform
  data_obj = JSON.parse(curl.body_str)

But, the JSON parser says its not a correct format. So, when I did a curl.body_str , I got this.

 => "\037\213\b\000\000\000\000\000\004\000\354\275\a`\034I\226%&/m\312{\177J\365J\327\340t\241\b\200`\023$\330\220@\020\354\301\210\315\346\222\354\035iG#)\253*\201\312eVe]f\026@\314\355\235\274\367\336{\357\275\367\336{\357\275\367\272;\235N'\367\337\377?\\fd\001l\366\316J\332\311\236!\200\252\310\037?~|\037?\"~\361o\234\244\351Gm\325f\345G\217\322\335\021\377\271\312.\362\360\257\246\370\001>\271\267#\037\255\233\274n\350\357\357\341\2574\375\305\362C\277\370\375\213\031}\265wo\367\341\356\203Q\370M{\275\002\230\217\352\374\242h\332\274\316g\037\271\026\323:\317\332\242Z\376.......

Any suggestions? How can I fetch a JSON file?

I am new to Ruby on Rails too, so is there any better gem to access the SO API?

SOLUTION:

This works, the problem was it was GZIP

  curl = Curl::Easy.new(url_so)
  curl.perform
  json_data = ActiveSupport::Gzip.decompress(curl.body_str)
0

5 Answers 5

2

I think your problem is that you're forgetting that the API only supplies gzipped data.

I don't use Ruby, so you'll have to figure out how to tell curl to accept gzip data.

2
2

From this link: Gzip and deflate compression in curb http driver

You can set curl.encoding = '' to make curb auto decompress response body. Like below:

url_so = "http://api.stackoverflow.com/1.0/users/231917?type=jsontext"
curl = Curl::Easy.new(url_so)

# enables both deflate and gzip compression of responses
curl.encoding = ''

curl.perform
1

Try to set the Accepted-encoding like:

curl = Curl::Easy.new(url_so)
curl.headers["Accept-Encoding"] = "gzip,deflate"
curl.perform
2
  • didn't try this, but this also might work.
    – zengr
    Aug 9, 2010 at 7:07
  • 1
    @zengr give it a shot :) Aug 9, 2010 at 7:14
0

Try it without the ?type=jsontext parameter.

2
0

Are you doing this to learn ruby, or get a Stack Apps app up and running?

If you're looking to just get an app up, try one of these libraries - make life easier for yourself :)

If you're looking to learn ruby (and thus don't want to use a library), well I've never used ruby so I can't help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .