4

このpost リクエストがあります:

curl -i -X POST \
   -H "Accept:application/json" \
   -H "content-type:application/x-www-form-urlencoded" \
   -d "disambiguator=Document&confidence=-1&support=-1&text=President%20Obama%20called%20Wednesday%20on%20Congress%20to%20extend%20a%20tax%20break%20for%20students%20included%20in%20last%20year%27s%20economic%20stimulus%20package" \
   http://spotlight.dbpedia.org/dev/rest/annotate/

rubyでどう書けばいいの?カイルが提案したように、私はこれを試しました:

require 'rubygems'
require 'net/http'
require 'uri'

uri = URI.parse('http://spotlight.dbpedia.org/rest/annotate')

http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({
                      "disambiguator" => "Document",
                      "confidence"    => "0.3",
                      "support"       => "0",
                      "text"          => "President Obama called Wednesday on Congress to extend a tax break for students included in last year's economic stimulus package"
                      })

request.add_field("Accept", "application/json")
request.add_field("Content-Type", "application/x-www-form-urlencoded")

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

しかし、このエラーが発生しました:

#<Net::HTTPInternalServerError 500 Internal Error readbody=true>
4

1 に答える 1

2

その後、私の最初の回答を含めるように質問を変更しました。私の最初の回答では、「Content-Type」ヘッダーを追加すると重複し、そのヘッダーの追加を削除すると、例が機能するはずです:

このチート シートでは、HTTP ポストにパラメーターを提供する方法について説明します。

require 'rubygems'
require 'net/http'
require 'uri'

uri = URI.parse('http://spotlight.dbpedia.org/rest/annotate/')

http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({
  "disambiguator" => "Document",
  "confidence"    => "0.25",
  "support"       => "0",
  "text"          => "President Obama called Wednesday on Congress to extend a tax break for students included in last year's economic stimulus package"
})

request.add_field("Accept", "application/json")

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

URLを「http://localhost:9999」に設定し、別の端末からnetcatを実行して、これを(Linuxシステムで)テストしました。

$ cat resp.txt
HTTP 200 OK


$ nc -l 9999 < resp.txt
POST /this/that HTTP/1.1
Content-Length: 223
Content-Type: application/x-www-form-urlencoded, application/x-www-form-urlencoded
Connection: close
Accept: */*, application/json
Host: localhost:9999

disambiguator=Document&confidence=0.25&support=0&text=President%20Obama%20called%20Wednesday%20on%20Congress%20to%20extend%20a%20tax%20break%20for%20students%20included%20in%20last%20year%27s%20economic%20stimulus%20package

これは、フォーム パラメータの送信と http ヘッダーの設定の両方のニーズをカバーするはずです。

編集 - 0x90まで:

それがすべきことです:

request.add_field("Accept", "application/json")

request.set_form_data({
  "disambiguator" => "Document",
  "confidence"    => "0.3",
  "support"       => "0",
  "text"          => "President Obama called Wednesday on Congress to extend a tax break for students included in last year's economic stimulus package",
  "Content-Type" => "application/x-www-form-urlencoded"
})
于 2012-12-01T15:27:56.013 に答える