简体   繁体   中英

not an HTTP URI

I am calling the circle.so api using ruby on a rails on a code that I got from postman.

require "uri"
require "net/http"

url = URI("hub.atlas.fm/api/v1/community_members")

http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Get.new(url)
request["Authorization"] = "phdbM12mVAiEyLF19UjoYdiU"

response = http.request(request)
puts response.read_body

While working perfectly in postman, It does not work on my app, with an error of

ArgumentError in TopicsController#index
not an HTTP URI

How do I solve this?

You will need to specify http:// in your url string

url = URI("http://hub.atlas.fm/api/v1/community_members")

If you ommit the scheme (http://) part, then ruby does not know what specific URI type this is.

2.6.3 :005 > url = URI("hub.atlas.fm/api/v1/community_members")
 => #<URI::Generic hub.atlas.fm/api/v1/community_members>

But if you specify the scheme (http://) then you'll get

2.6.3 :003 > url = URI("http://hub.atlas.fm/api/v1/community_members")
 => #<URI::HTTP http://hub.atlas.fm/api/v1/community_members>

Note the different class ( URI::Generic vs. URI::HTTP ).

There are many other types of URI, eg:

2.6.3 :007 > url = URI("file://hub.atlas.fm/api/v1/community_members")
 => #<URI::File file://hub.atlas.fm/api/v1/community_members>
2.6.3 :008 > url = URI("ftp://hub.atlas.fm/api/v1/community_members")
 => #<URI::FTP ftp://hub.atlas.fm/api/v1/community_members>

You can find more here: https://ruby-doc.org/stdlib-2.7.2/libdoc/uri/rdoc/URI.html

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM