質問

私は使用しようとしています 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 Typoを修正した後、私は今取得します:

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>
役に立ちましたか?

解決

あなたは彼らに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'}

他のヒント

おそらく単なるタイプミス? @host あるべきです host 右?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top