0

I am getting this following error when making a POST request to my SPARQL Client endpoint:

Malformed query: org.openrdf.rio.RDFParseException: IRI included an unencoded space: '32' [line 1]

I am using Postman to make the request and have set a single header which is Content-Type and has the value sparql-update.

The data that I am passing in the body is like this:

insert data { <rdf:RDF xmlns:html="http://www.w3.org/1999/xhtml"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:country="http://example.xyz.ds/country/"
         xmlns:currency="http://example.xyz.ds/currency/"
         xmlns:area="http://example.xyz.ds/area/"
         xmlns:city="http://example.xyz.ds/city/"
         xmlns:root="http://example.xyz.ds/terms#"
         xmlns:specialIdenifier="http://example.xyz.ds/specialIdenifier/"
         xmlns:product="http://example.xyz.ds/product/"
         xmlns:company="http://example.xyz.ds/company/"
         xmlns:office="http://example.xyz.ds/office/"
         xmlns:schema="http://schema.org/"
         xmlns:uuid="java:java.util.UUID"
         xmlns:acmUtil="http://example.xyz.ds/util-functions">
    <rdf:Description rdf:about="http://example.xyz.ds/company/123456789>
        <rdf:type rdf:resource="http://example.xyz.ds/terms#company"/>
        <rdfs:label/>
        <root:fid xmlns:urn="urn:"
                  xmlns:func="http://example.xyz.ds/func"
                  rdf:datatype="http://www.w3.org/2001/XMLSchema#string">4049</root:fid>
        <root:deleted xmlns:urn="urn:"
                      xmlns:func="http://example.xyz.ds/func"
                      rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">false</root:deleted>
    </rdf:Description>
</rdf:RDF> }

I am hoping that I can just POST this entire RDF XML document in the body and the SPARQL client/Neptune database will just understand the triples within it.

I have seen a lot online about this error but can't find a solution related to POSTing the data directly. Any idea how to solve this problem?

Haych
  • 932
  • 13
  • 36
  • It whould not work - simply, it is not a valid SPARQL update syntactically. Check the documentaion of your endpoint for an alternative e.g. something like RDF4J's `statements` or graph-store protocol implementation. – Damyan Ognyanov Jun 26 '19 at 09:31
  • Are you saying that it is not possible to post an entire RDF document like this? I would need to break in down into RDF triples or something like that? Or it is possible to do and I have just done it incorrectly? – Haych Jun 26 '19 at 09:35
  • 1
    What you did, was to wrap a complete piece or RDF/XML serialized document into an `INSERT DATA` SPARQL update which is syntactically invalid. One way around is to use Turtle serialization of the document but place the `PREFIX` declarations outside of the `insert data block` as it is per sparql syntax. The other way, is to explore what Neptune provides - e.g. check the online docs at: https://docs.aws.amazon.com/neptune/latest/userguide/sparql-api-reference-update-load.html – Damyan Ognyanov Jun 26 '19 at 09:46
  • Can that Turtle serialization be done by the SPARQL query? Or would it need to be done in the code that makes the request? – Haych Jun 26 '19 at 09:58
  • No, it cannot be done using just SPARQL, so the code that prepares the update must handle it. What do you use to access the endpoint? Using java and 'SparqlRepository' or something else? If it is `SparqlRepository` there is a method `add` in connection instance which will do it but the parsing will be done at client side so that is a bit inneficient... – Damyan Ognyanov Jun 26 '19 at 10:23
  • 1
    @Haych why are you not using the `UPDATE LOAD` as suggested in the [Neptune docs](https://docs.aws.amazon.com/neptune/latest/userguide/sparql-api-reference-update-load.html) to upload a file? – UninformedUser Jun 26 '19 at 11:30
  • Thanks @DamyanOgnyanov I will take a look into that. – Haych Jun 26 '19 at 12:46
  • @AKSW That doc shows that I need to load data from a particular file. I want to do it from the Body of the method. Know if that is possible with UPDATE LOAD? – Haych Jun 26 '19 at 12:49

1 Answers1

1

As discussed in the comments, your SPARQL command is not syntactically valid: you can't just paste an RDF/XML document inside a SPARQL INSERT DATA command (see the SPARQL Update specification for details on correct syntax).

I am hoping that I can just POST this entire RDF XML document in the body and the SPARQL client/Neptune database will just understand the triples within it.

Unfortunately, Neptune has no option for that (at least, not as far as I can see from the Neptune docs).

You can upload an RDF/XML document to Neptune by using the UPDATE LOAD command, as shown in the Neptune documentation. If that is not an option for you, you will need to convert the data in the document to correct SPARQL syntax somehow.

There are several ways to to this, but if you're working in Java, one possible option is to use the rdf4j Repository API. You can connect to your Neptune instance's SPARQL endpoint using the SPARQLRepository class, as follows:

Repository rep = new SPARQLRepository("https://your-neptune-endpoint:port/sparql");
rep.init();

and then you can just open a connection and load your file that way:

try(RepositoryConnection conn = rep.getConnection()) {
      File file = new File("/my/local/rdf-xml-file.rdf");
      conn.add(file, "", RDFFormat.RDFXML);
}

Rdf4j will take care of converting your command into a SPARQL update request that the Neptune server can understand (slight caveat: I haven't tested this myself with Neptune, but if it is compliant with the SPARQL W3C Recommendations this should work).

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73