[英]How to translate a POST request in curl into ruby?
I have this post
request :我有这个
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/
How can I write it in ruby?我怎样才能用 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>
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:
在我最初的回答中,添加 'Content-Type' 标题会导致它被复制,删除该标题的添加,您的示例应该可以工作:
This Cheat Sheet describes how to provide parameters to HTTP posts:此备忘单描述了如何为 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
I tested this (on a Linux system) by setting the url to 'http://localhost:9999' and running netcat from another terminal:我通过将 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
This should cover both of your needs: sending form parameters as well as setting http headers.这应该满足您的两个需求:发送表单参数以及设置 http 标头。
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"
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.