I am accessing one of my Rails actions from the Rails console:
> @consumer = OAuth::Consumer.new(
> x,
> x,
> site: "http://localhost:3000"
> )
> request = @consumer.create_signed_request(:get,"/get_user_info?email=x")
> uri = URI.parse("http://localhost:3000")
> http = Net::HTTP.new(uri.host, uri.port)
> http.request(request).body
=> "{\"first_thing\":\"Nick\",\"second_thing\":\"2012-12-26T11:41:11Z\",\"third_thing\":\"2012-12-26T11:40:03Z\"}"
> http.request(request).body.class
=> String
The action is supposed to be returning a hash in JSON, not a string. Here is the how the action ends:
render json: {
first_thing: x,
second_thing: x,
third_thing: x
}
Why would this be coming through as a string? I'm using Rails 3.2.0 and Ruby 1.9.3.
You will always get a String from an HTTP request. render :json
just converts the hash into a JSON String.
You need to do JSON.parse
on the String.
It return JSON, because whole HTTP messages are string based. So to get JSON object in console you need call JSON.parse
on response body.
JSON.parse(http.request(request).body) # => {
# first_thing: x,
# second_thing: x,
# third_thing: x
#}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.