Reputation: 41022
I have this post
request:
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/
How can I write it in ruby? I tried this as Kyle had suggested:
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
but got this error:
#<Net::HTTPInternalServerError 500 Internal Error readbody=true>
Upvotes: 5
Views: 6779
Reputation: 27588
You've since modified your question to include my initial response. In my initial answer, adding in the 'Content-Type' header causes it to be duplicated, remove the adding of that header and your example should work:
This Cheat Sheet describes how to provide parameters to HTTP posts:
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
I tested this (on a Linux system) by setting the url to 'http://localhost:9999' and running netcat from another terminal:
$ 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
This should cover both of your needs: sending form parameters as well as setting http headers.
that is what should be done:
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"
})
Upvotes: 3