Search code examples
ruby-on-railsrubyruby-on-rails-3api-designapi-doc

a specific api that search imagen in ruby didn t work to me


i am trying get the response of this api: here is some documentation of the api
so when i try to call the api, the console give me the following menssage:

web_1       | #<Unirest::HttpResponse:0x000055ac03684818 @code=200,@headers={:d
ate=>"Sat, 23 Jun 2018 06:17:30 GMT", :server=>"Microsoft-IIS/8.5", :cache_contr
ol=>"no-cache", :pragma=>"no-cache", :content_length=>"29", :content_type=>"appl
ication/json; charset=utf-8", :expires=>"-1", :x_aspnet_version=>"4.0.30319", :x
_powered_by=>"ASP.NET"}, @raw_body=<RestClient::Response 200 "{\"_type\":\"i..."
>, @body={"_type"=>"images", "value"=>[]}>

but if you see the "value" where the url of the imagens must be, it is empty and i don t know why, this is my code:

require 'json'
require 'unirest'
response = Unirest.get "http://contextualwebsearch.com/api/Search/ImageSearchAPI?q=donald%20trump&count=10&autoCorrect=true"
puts response.inspect

and the rare of all of this, it is that if you go to this page: http://contextualwebsearch.com/api/Search/ImageSearchAPI?q=donald%20trump&count=10&autoCorrect=true you will find the json with all the information that i am searching.


Solution

  • You can use rest-client

    require 'net/http' 
    require 'json'
    require 'rest-client'
    res = RestClient.get("http://contextualwebsearch.com/api/Search/ImageSearchAPI?q=donald%20trump&count=10&autoCorrect=true")
    body = JSON.parse(res, { symbolize_names: true })
    puts body[:value]
    

    Also Unirest works fine

    require 'json'
    require 'unirest'
    response = Unirest.get "http://contextualwebsearch.com/api/Search/ImageSearchAPI?q=donald%20trump&count=10&autoCorrect=true"
    puts response.body["value"]