Skip to content

Instantly share code, notes, and snippets.

@phlco
Last active December 24, 2015 18:19
Show Gist options
  • Select an option

  • Save phlco/6842434 to your computer and use it in GitHub Desktop.

Select an option

Save phlco/6842434 to your computer and use it in GitHub Desktop.

Homework

The fix

Add the following to the top of your main.rb file

require 'json'

And then add the following to your response back from HTTParty.

parsed_result = JSON.parse(response.body)

What's going on?

The reason we're getting back a string and not a nice hash is because OpenMovieDB is sending back incorrect response headers. We learned this week that HTTP responses contain the following:

Status-Line
Headers
(empty line)
Message

The headers portion contains the MIME or content-type that tells the browser how to interpret the message. The correct content type for JSON is "application/json; charset=utf-8".

The response back from the OpenMovieDB is "text/html; charset=utf-8" which is why we're getting back a string. UGH.

What's the difference?

To demonstrate the difference we could try the following:

In pry...

open_movie_url = "http://www.omdbapi.com/?t=True%20Grit&y=1969"
open_movie_response = HTTParty.get(open_movie_url)
open_movie_response.headers['content-type']
#=> "text/html; charset=utf-8"
other_json_url = "http://api.randomuser.me/0.2/"
other_json_response = HTTParty.get(other_json_url)
other_json_response.headers['content-type']
#=> "application/json; charset=utf-8"

We could also see this in Chrome through the Network tab. We'd see the type for OmdbApi listed as text/hml and our other example as application/json

How would we try to figure this out?

If we don't know about response headers or how to check those with HTTParty, what could we do?

Something we need to stress is how we might solve a problem we don't know the answer to.

We know earlier that Jonathan was able to get back a hash from HTTParty when he did get requests to Facebook and for the Stock exercise, so something feels off. Some questions we might ask ourselves are "What does HTTParty return or what is it supposed to return?"

If we check it out in Pry…

response.class returns HTTParty::Response which doesn't tell us as much.

I could cd into the response and poke around with ls at all the methods, but I might not know what I'm looking for.

I could search what is HTTParty::Response which leads me to this documentation on HTTParty/Response that looks AWFUL to read.

I could try searching what does httparty return and find this stack overflow answer.

That includes a link to this blog post which includes a paragraph that says:

"One of the coolest features of HTTParty is that it automatically parses JSON and XML responses, based on the Content-Type of the response, so as soon as your response comes back it’s ready for you to work with it:"

What? No it doesn't! What is this person talking about?!

Well I think I want JSON, because I want a hash of the data, and it doesn't look like it's automatically getting parsed. So I could search why is httparty returning a string and find this stackoverflow question.

This person seems to be having the same problem. They tried using require 'json' and JSON.parse. I could guess from our other ruby apps that require 'json' would probably go in my main.rb file maybe with require 'sinatra' and all those.

But maybe I don't know how JSON.parse works.

If I search for JSON.parse it brings up this link, but that's all about JavaScript. So maybe I search for ruby JSON.parse and find this link which includes this line JSON.parse(string)

I'd try all these things out in Pry to make sure I get a workable hash.

PHEW.

Note JSON.parse() works on both the response and response.body the response.to_s method returns the body. It's probably best to stay explicit and use JSON.parse(response.body) but either works.

###Extra Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment