gpt4 book ai didi

ruby - POST(而非 GET)维基数据查询

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

我正在关注 these instructions提交 SPARQL 查询,首选 POST 方法,因为查询可能很长。但即使 GET 有效,它似乎也失败了 - 有什么方法可以使 POST 查询有效?

sparql = <<END
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
END

# Fails with 405 Not writable
resp = Excon.post('https://query.wikidata.org/sparql', body: "query=#{URI::encode(sparql)}")
puts resp.status, resp.body

# Works with 200
resp = Excon.get("https://query.wikidata.org/sparql?query=#{URI::encode(sparql)}")
puts resp.status, resp.body

我也试过发送 "Content-Type"=> "application/x-www-form-urlencoded",但没有成功。

最佳答案

感谢上面的评论者展示了工作示例。我根据所有明显的组合做了一些进一步的检查,如下所示。

总结:

  • 如果 GET 很短且可能被重用(因为 GET 查询已缓存),请使用 GET
  • 确保您发送了一个 user-agent header ,并注意某些库默认情况下不会包含一个 header (否则它会返回无法解释的 403)
  • 对于 POST,最好和最简单的(在我看来)是将 SPARQL 查询作为整个正文发送,“content-type”为“application/sparql-query”,此处不需要对查询进行编码。您也可以使用“content-type”作为“application/x-www-form-urlencoded”的表单语法,并确保查询已编码。
require 'excon'

# any arbitrary query
sparql = 'SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }'

# SUCCESS!
resp = Excon.get("https://query.wikidata.org/sparql?query=#{URI::encode(sparql)}")
puts "GET", resp.status, resp.body[0,100], "\n"

# FAIL! 403 (need user agent)
headers = { "Content-Type" => "application/sparql-query" }
resp= Excon.post('https://query.wikidata.org/sparql', body: sparql, headers: headers)
puts "POST sparql-query", resp.status, resp.body[0,100], "\n"

# SUCCESS!
headers = { "Content-Type" => "application/sparql-query", "User-Agent" => "Ruby 2.6.4" }
resp= Excon.post('https://query.wikidata.org/sparql', body: sparql, headers: headers)
puts "POST sparql-query with user-agent", resp.status, resp.body[0,100], "\n"

# FAIL! 403 (need user agent)
headers = { "Content-Type" => "application/x-www-form-urlencoded" }
resp = Excon.post('https://query.wikidata.org/sparql', body: "query=#{URI::encode(sparql)}", headers: headers)
puts "POST form", resp.status, resp.body[0,100], "\n"

# SUCCESS!
headers = { "Content-Type" => "application/x-www-form-urlencoded", "User-Agent" => "Ruby 2.6.4" }
resp = Excon.post('https://query.wikidata.org/sparql', body: "query=#{URI::encode(sparql)}", headers: headers)
puts "POST form with user-agent", resp.status, resp.body[0,100], "\n"

# FAIL! 405 (need encoding)
resp = Excon.post('https://query.wikidata.org/sparql', body: "query=#{URI::encode(sparql)}")
puts "POST plain", resp.status, resp.body[0,100], "\n"

# FAIL! 405 (need encoding)
headers = { "User-Agent" => "Ruby 2.6.4" }
resp = Excon.post('https://query.wikidata.org/sparql', body: "query=#{URI::encode(sparql)}", headers: headers)
puts "POST plain with user-agent", resp.status, resp.body[0,100], "\n"

更多信息

关于ruby - POST(而非 GET)维基数据查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58041367/

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