在 Ruby 中与 Yahoo Placemaker 交互

发布于 2025-01-07 06:47:44 字数 2119 浏览 1 评论 0原文

我正在尝试使用 net/http 与 Yahoo Placemaker API 进行交互,但我似乎无法让它工作。这是我到目前为止所得到的:

host = 'wherein.yahooapis.com'
payload = {
    'documentContent' => 'Columbus Ohio',
    'appid' => APP_ID,
    'outputType' => 'json',
    'documentType' => 'text/plain'
}.to_json

req = Net::HTTP::Post.new('/v1/document', initheader = { 'Content-Type' =>'application/json'})
req.body = payload
response = Net::HTTP.new(host).start {|http| http.request(req) }
puts "Response #{response.code} #{response.message}: #{response.body}"

这给了我以下错误:

Errno::ECONNREFUSED: Connection refused - connect(2)
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `open'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:744:in `start'
    from /Users/Kyle/Desktop/skateparks-web/lib/yahoo/placemaker.rb:21:in `extract'
    from (irb):3
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'

修复@host拼写错误后,我现在得到:

Response 400 Bad Request:
<?xml version="1.0" encoding="UTF-8" ?>
<yahoo:error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns:cle="http://wherein.yahooapis.com/v1/schema.rng" xmlns:xml="http://www.w3.org/XML/1998/namespace" xml:lang="en"><yahoo:description><![CDATA[Please provide a document URL or content.]]></yahoo:description>
<yahoo:detail><code>-9999</code>
<cause>input</cause>
</yahoo:detail></yahoo:error>

I'm trying to use net/http to interact w/ the Yahoo Placemaker API but I can't seem to get it to work. Here is what I have so far:

host = 'wherein.yahooapis.com'
payload = {
    'documentContent' => 'Columbus Ohio',
    'appid' => APP_ID,
    'outputType' => 'json',
    'documentType' => 'text/plain'
}.to_json

req = Net::HTTP::Post.new('/v1/document', initheader = { 'Content-Type' =>'application/json'})
req.body = payload
response = Net::HTTP.new(host).start {|http| http.request(req) }
puts "Response #{response.code} #{response.message}: #{response.body}"

This gives me the following error:

Errno::ECONNREFUSED: Connection refused - connect(2)
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `open'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:744:in `start'
    from /Users/Kyle/Desktop/skateparks-web/lib/yahoo/placemaker.rb:21:in `extract'
    from (irb):3
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'

After fixing the @host typo I now get:

Response 400 Bad Request:
<?xml version="1.0" encoding="UTF-8" ?>
<yahoo:error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns:cle="http://wherein.yahooapis.com/v1/schema.rng" xmlns:xml="http://www.w3.org/XML/1998/namespace" xml:lang="en"><yahoo:description><![CDATA[Please provide a document URL or content.]]></yahoo:description>
<yahoo:detail><code>-9999</code>
<cause>input</cause>
</yahoo:detail></yahoo:error>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

前事休说 2025-01-14 06:47:44

您向他们发送 json,他们似乎在帖子正文中寻找常规查询字符串。另外,您可能应该使用 httparty 来使用 Web 服务,除非您有充分的理由不这样做:

require 'httparty'

class Yahoo
    include HTTParty
    base_uri 'http://wherein.yahooapis.com/'
end

json = Yahoo.post '/v1/document', :body => {:documentContent => 'Columbus Ohio', :appid => APP_ID, :outputType => 'json', :documentType => 'text/plain'}

You're sending them json and they seem to be looking for a regular query string in the post body. Also you should probably use httparty to consume web services unless you have a good reason not to:

require 'httparty'

class Yahoo
    include HTTParty
    base_uri 'http://wherein.yahooapis.com/'
end

json = Yahoo.post '/v1/document', :body => {:documentContent => 'Columbus Ohio', :appid => APP_ID, :outputType => 'json', :documentType => 'text/plain'}
心如荒岛 2025-01-14 06:47:44

可能只是一个错字? @host 应该是 host 对吗?

Possibly just a typo? @host should be host right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文