POST(不是GET)维基数据查询

POST (not GET) wikidata query

我正在按照 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 并注意某些库默认情况下不会包含一个(否则 returns 无法解释的 403)
  • 对于 POST,最好和最简单的(在我看来)是将 SPARQL 查询作为整个 body 发送,"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"

更多信息