POST(非 GET)维基数据查询

mah*_*off 4 ruby sparql wikidata

我正在按照这些说明提交 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
Run Code Online (Sandbox Code Playgroud)

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

mah*_*off 6

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

总之:

  • 如果它很短并且可能被重用,请使用 GET(因为 GET 查询被缓存)
  • 确保您发送一个user-agent标头并注意某些库默认不会包含一个标头(否则它会返回一个无法解释的 403)
  • 对于 POST,最好和最简单的(在我看来)是将 SPARQL 查询作为整体发送,“内容类型”作为“应用程序/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"
Run Code Online (Sandbox Code Playgroud)

更多信息