HTTP Error 414 with query to DBPedia endpoint using SPARQLwrapper - http

I made a function that executes a SPARQL query on the DBpedia SPARQL endpoint. This function takes an array of 15 elements, and each time it substitutes an element from the array into the query, then executes it to get result. The problem is that it takes the first 9 elements then this error is raised:
results = sparql.query().convert()
File "build/bdist.linux-i686/egg/SPARQLWrapper/Wrapper.py", line 390, in query
return QueryResult(self._query())
File "build/bdist.linux-i686/egg/SPARQLWrapper/Wrapper.py", line 369, in _query
raise e
HTTPError: HTTP Error 414: Request-URI Too Large
My query is as follows:
sparql = SPARQLWrapper('http://mlode.nlp2rdf.org/sparql');
querystring="""
PREFIX dc:<http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX olia-ar: <http://purl.org/olia/arabic_khoja.owl#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lexvo: <http://lexvo.org/id/iso639-3/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX gold: <http://purl.org/linguistics/gold/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX qvoc: <http://www.nlp2rdf.org/quranvocab#>
SELECT ?verseTextAr ?tafseer
WHERE
{
?verse a qvoc:Verse;
qvoc:chapterIndex 26;
qvoc:verseIndex WORD;
skos:prefLabel ?verseTextAr;
qvoc:descByJalalayn ?tafseer.
}
"""

The 414 error means that SPARQLWrapper is trying to do a HTTP GET for the query but the query is too large resulting in a request URI that the DBPedia servers reject.
You need to get SPARQLWrapper to POST the query instead, the documentation states that this is possible and it appears that the setMethod() method should be used to configure this.

Related

How to get first item from array in Robot Framework

I have the following response from a POST request:
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}
So I used the "Convert String to JSON" function and got the following response:
{'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4',
'description': 'luovbfvwofgdrcwvqtyqohjioocszgplcjh',
'hasAnyPartnership': False, 'hasAnyProcedure': False}
How do I get the ID value that is inside FACILITIES?
'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4'
The JSON example you have provided is not the valid one. It is missing ] of facilities array and } of opening external brace. After correction it should look like this -
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}]}
You can use following keywords from JSONLibrary
${json}= Convert String to JSON ${JsonVar}
${idValue}= Get Value From Json ${json} $.facilities[0].id
Output -

No route matches {:action=>"show", :controller=>"products"}, missing required keys: [:id]

I have a rSpec Controller test which fails
the index route will have an :id which does not exist in an index route.
I've clean routes:
resources :products
This is the controller,
class ProductsController < ApplicationController
before_action :set_product, only: [:show]
skip_before_action :authorize_request, only: [:show, :index]
# GET /products
def index
# params here {"controller"=>"products", "action"=>"index", "product"=>{}}
#products = Product.all
render json: #products
end
ActionController::UrlGenerationError: No route matches {
:action=>"show",
:controller=>"products"},
missing required keys: [:id]
Why is "show" called? I did not passed any params to the controller:
This is the spec:
RSpec.describe "/products", type: :request do
describe " GET /index" do
it " renders a successful response " do
Fabricate.times(3, :product)
get "/products", headers: valid_headers
expect(response).to be_successful
end
end
end
When I change the routes from get "/products", to: 'products#index' and comment out resources :products then it passes the test
EDIT / PROBLEM FOUND :
I use include Rails.application.routes.url_helpers in my Product model, which caused this issue. I need it to generate URLs to retrieve my attachments. How else can I get the URLs of ActiveStorage ?
I solved the problem: I had include Rails.application.routes.url_helpers in my Product model which caused the problem:
class Product < ApplicationRecord
# include Rails.application.routes.url_helpers
end
commented out, all specs are passing now
But I still don't understand why I can't use it in my Model.
I wanna have it included to retrieve urls of my attachments:
def main_image_thumb_url
rails_blob_url(main_image, host: "localhost:3000")
end

Nginx rewrite by map with parameters

I would like to rewrite with map as
/oldpage?f=regist -> /signup
/oldpage?f=regist&a=1 -> /signup?a=1
/oldpage?f=confirm -> /signup?t=confirm
/oldpage?f=confirm&a=1 -> /signup?t=confirm&a=1
but my redirect result in nginx (v1.12.2) is
/oldpage?f=regist -> /signup?f=regist
/oldpage?f=regist&a=1 -> Not Found
/oldpage?f=confirm -> /signup?t=confirm?f=confirm
/oldpage?f=confirm&a=1 -> Not Found
I set nginx.conf as,
map $request_uri $rewrite_uri {
include conf.d/redirect.map;
}
server {
...
if ($rewrite_uri) {
rewrite ^ $rewrite_uri redirect;
}
}
and redirect.map is
/oldpage?f=regist /signup;
/oldpage?f=confirm /signup?t=confirm;
It would be really appreciated if you could give me some advices.
If the a=1 parameter represents any other parameters, and you do not wish to add those combinations to the map file, you should change the syntax of your map file to use regular expressions.
The regular expressions in the map block can create named captures which can be used later in the configuration. In the example below, the $prefix and $suffix variables are named captures from the map block.
The example below has some caveats - because the $prefix and $suffix values may be empty, the generated URIs may contain a trailing ? or & - which should not affect the overall semantics.
All of the regular expressions and the mapped values have a common pattern to capture optional parameters and append them to the resulting value.
map $request_uri $new {
default 0;
~*^/oldpage\?(?<prefix>.*&)?f=regist(&(?<suffix>.*))?$ /signup?;
~*^/oldpage\?(?<prefix>.*&)?f=confirm(&(?<suffix>.*))?$ /signup?t=confirm&;
}
server {
...
if ($new) {
return 301 $new$prefix$suffix;
}
See this document for more.

How to filter non-resolvable URIs on a SPARQL query?

Is it possibe to filter out results that contains a non-resolvable URI within the SPARQL query?
An example: I'm making the following query (endpoint: http://linkeddata.systems:8890/sparql):
PREFIX RO: <http://www.obofoundry.org/ro/ro.owl#>
PREFIX SIO: <http://semanticscience.org/resource/>
PREFIX EDAM: <http://edamontology.org/>
PREFIX PHIO: <http://linkeddata.systems/ontologies/SemanticPHIBase#>
PREFIX PUBMED: <http://linkedlifedata.com/resource/pubmed/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX up: <http://purl.uniprot.org/core/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?disn_1 ?label ?rel ?valor
WHERE { ?disn_1 ?rel ?valor . ?disn_1 rdfs:label ?label FILTER(( ?disn_1 = <http://linkeddata.systems/SemanticPHIBase/Resource/host/HOST_00561>))}
In the results, as you can see there is in ?valor variable a triple that contains a non-resolvable URI (text: /hostncbitaxid/). I would like to know if there is some specific FILTER that can be added in the SPARQL query to remove those results with non-resolvable URIs.
I'm having problems with the API that I'm using to process these results in C# because it is returning an exception due to non-resolvable URIs so I would like to filter them out in the SPARQL query (if possible).
How do you know that it's not resolvable? RDF doesn't have a concept of a "relative URI", all the URIs are resolved relative to something (and perhaps to what is an implementation detail in some cases), so you end up with absolute URIs. In the HTML results from that endpoint, I get http://linkeddata.systems:8890/hostncbitaxid/, and that could easily be resolvable.
That said, if you are ending up with results that include non-absolute URIs, and you want to filter those out, you could use some heuristics to do that. For instance, if you only want URIs beginning with http, you can do that. E.g., here's a query that returns two values for ?uri:
prefix : <urn:ex:>
select * where {
values ?uri { <http://www.example.org/> </foobar> }
}
-----------------------------
| uri |
=============================
| <http://www.example.org/> |
| <file:///foobar> |
-----------------------------
(Notice that the relative URI /foobar got resolved as a file:// URI.) You can keep only http URIs with a filter:
prefix : <urn:ex:>
select * where {
values ?uri { <http://www.example.org/> </foobar> }
filter strstarts(str(?uri), "http")
}
-----------------------------
| uri |
=============================
| <http://www.example.org/> |
-----------------------------
The query returns (SPARQL results in JSON format):
"valor": { "type": "uri", "value": "/hostncbitaxid/" }}
This is bad data - it must be an absolute URI in RDF. Presumably the data is bad. You can remove it in the query as #joshua-taylor shows.

Edit property of a child using REST Api

I have a dataset uploaded in following form:
SomeKey
-> Alphabet
-Emp1: "{ 'Fname' : 'Bob', 'Lname' : 'Sob' }"
-Emp2: "{ 'Fname' : 'Tom', 'Lname' : 'Mot }"
Now using Rest API, I want to edit Fname and Lname of employee with key Emp1 to Fred and Dref, and Fname and Lname of employee with key Emp2 to Kent and Tenk in one single call. Is this possible? If yes, how?
If you would like to update a single employee, you can do so with a PUT REST API call like this:
curl -X PUT -d <data> https://some.url.com/SomeKey/Employees/Emp1/.json
If would like to update multiple employees using a single REST API call, you can do so with a PATCH REST call like this:
curl -X PUT -d '{"Emp1":<data1>,"Emp2":<data2>"}' \
https://some.url.com/SomeKey/Employees/.json
Additional details about the REST API are available here:
https://www.firebase.com/docs/rest-api.html

Resources