在 Ruby 中与 Yahoo Placemaker 交互
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您向他们发送 json,他们似乎在帖子正文中寻找常规查询字符串。另外,您可能应该使用 httparty 来使用 Web 服务,除非您有充分的理由不这样做:
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:
可能只是一个错字?
@host
应该是host
对吗?Possibly just a typo?
@host
should behost
right?