gpt4 book ai didi

ruby-on-rails - 如何将 curl 中的 POST 请求转换为 ruby​​?

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:04 25 4
gpt4 key购买 nike

我有这个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/

我怎样才能用 ruby​​ 写它?我按照 Kyle 的建议尝试了这个:

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>

最佳答案

您已经修改了您的问题以包含我的初始回复。在我最初的回答中,添加“Content-Type” header 会导致它被复制,删除该 header 的添加,您的示例应该可以工作:

This Cheat Sheet描述了如何向 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 header 。

编辑-按 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"
})

关于ruby-on-rails - 如何将 curl 中的 POST 请求转换为 ruby​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com