Programmaticaly upload dataset to fuseki - sparql

I use the jena fuseki 2 docker image to create a fuseki server.
And I want to know if there is a way to upload my dataset to fuseki not from the web interface but programmatically, from SPARQL or Python or whatever else.
And also, is there a way to work with ontology from webprotégé directly from fuseki?
Thanks for your answer

Fuseki comes with an HTTP API that can be used to upload data. You could use CURL or a Python HTTP library to communicate with this API. Fuseki also includes command-line helper scripts that can be used for calling the HTTP API. See https://jena.apache.org/documentation/fuseki2/soh.html#soh-sparql-http for more details.

If your RDF data is in turtle format, you can use the following code:
data = open('test.ttl').read()
headers = {'Content-Type': 'text/turtle;charset=utf-8'}
r = requests.post('http://localhost:3030/mydataset/data?default', data=data, headers=headers)
If your RDF data are in other format, you should change your headers, here is a list:
n3: text/n3; charset=utf-8
nt: text/plain
rdf: application/rdf+xml
owl: application/rdf+xml
nq: application/n-quads
trig: application/trig
jsonld: application/ld+json

I tried to upload file to fuseki using CURL, WGET, ./s-post, ./s-put with no effect. I generated request with postman's help. If someone, like me, is looking for correct CURL request, this is it:
curl --location --request POST 'http://{FUSEKIADDRESS}/{YOURDATASET}/data' --header 'Content-Type: multipart/form-data' --form 'file.ttl=#{}PATHtoFILE/file.ttl'

Related

How can this curl command be converted to pycurl or python requests?

any hel pwill be highly appreciated. Just can't make this to work. I am basically trying to update my Grafana dashboard via a http request. managed to get it to work with curl but want to do this with python's requests or pycurl.
curl -X PUT https://<api token>#ks.hostedgraphite.com/api/v2/grafana/dashboards/<my_dashboard> --data-binary #dashboard.json
The command above works. Tried several ways, an example of a code snippet:
crl = pycurl.Curl()
crl.setopt(crl.URL,
'https://apitoken#ks.hostedgraphite.com/api/v2/grafana/dashboards/<my_dashborad>')
crl.setopt(crl.UPLOAD, 1)
file = open('dashboard.json')
crl.setopt(crl.READDATA, file)
crl.perform()
crl.close()
file.close()
print('Status: {}'.format(crl.getinfo(crl.RESPONSE_CODE)))
curl -X PUT https://api_token#ks.hostedgraphite.com/api/v2/grafana/dashboards/my_dashboard --data-binary #dashboard.json
translates to
import requests
data = open('dashboard.json', 'rb').read()
response = requests.put('https://api_token#ks.hostedgraphite.com/api/v2/grafana/dashboards/my_dashboard', data=data)
Just replace proper values for api_token and my_dashboard.
You can use https://curl.trillworks.com/ for converting curl commands to equivalent code using python requests.
ok, so my main issue was creating a http request via python requests.
Figured it out, I setthe auth param to auth=("graphite_api_token","") and it worked:
data = json.dumps(dashbord_dict)
headers = {"Accept": "application/json","Content-Type":"application/json"}
response =requests.put('https://ks.hostedgraphite.com/api/v2/grafana/dashboards/some_dashboard,auth=("graphite_api_token",""),data=data,headers=headers)
print(response.status_code)

Configure Karate Mock to work with API's having Content Type other than JSON, XML

I need to mock an API using Karate Mock which accepts messages with Content-Type as below and respond accordingly
'Content-Type: application/x-www-form-urlencoded'
I tried to configure it in feature file as below but encountered below issue. I guess by default it is accepting JSON as the content-type, Is there any way to configure mock to read custom messages like this one.
Actual API call:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "elem_value=generic_value&address=" http://myhost:9090/api/resource_path
Mock feature file:
Scenario: pathMatches('/api/resource_path') && typeContains('urlencoded')
* def responseStatus = 200
* def response = read ('/payload/Success.json')
* print response
Actual Error:
Caused by: <eval>:1 ReferenceError: "generic_value" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1441)
at jdk.nashorn.internal.scripts.Script$43$\^eval\_.:program(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
This should have been fixed in a newer version of Karate, please try upgrade.
I think it will be in 0.9.2 - https://github.com/intuit/karate/issues/649

NiFi- how to http post a PDF document

I wanted to use NiFi's posthttp/invokeHttp processor to post a PDF to an API.
But considering the following cURL request to replicate in NiFi:
curl -X POST "http://ipaddress:port/api/" -H "accept: application/json" -H
"Content-Type: multipart/form-data" -F "pdf_file=#sample.pdf;
type=application/pdf"
Which property takes the -F information in nifi attributes?
Configuration for invokehttp right now:
error:
"400 Bad Request: The browser (or proxy) sent a request that this server could not understand."
Configration for posthttp right now:
error:
server logs: readv() failed (104: Connection reset by peer) while reading upstream
In older version of nifi you will have to use your own script to build a multipart request and then use invoke to create post request. You can refer to this post for a ExecuteGroovyScript example.
https://stackoverflow.com/a/57204862
Since Nifi 1.12 you can directly use invokeHTTP by setting content-type
https://stackoverflow.com/a/69284300
When you use PostHttp/InvokeHttp you wouldn't be referencing an external file, you would be sending the content of the flow file. So you would first need to bring sample.pdf into NiFi by using GetFile or ListFile/FetchFile and then flow file coming out of those processors represents the PDF, and you would route that flow file to InvokeHttp which would POST the content of the flow file (the pdf).

how to set the audio file sample rate for watson text to speech in node-red

watson text to speech accepts extra parameters such as the sampling (default is 44khz)
see https://www.ibm.com/watson/developercloud/text-to-speech/api/v1/#synthesize_audio for options such as this one
(...)
audio/l16;rate=rate
(You can optionally specify endianness=big-endian
or endianness=little-endian; the default is little endian.)
(...)
but I can't see a way to set these options in node red
In the Node-red, doesn't have the option, because you need to add in your cURL.
As you can see, you can use Accept or accept query parameter header to specify the audio format.
For example:
curl -X POST -u "{username}":"{password}"
--header "Content-Type: application/json"
--header "Accept: audio/l16;endianness=big-endian"
--data "{\"text\":\"Hello world\"}"
--output hello_world.wav
"https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice"
See the Official Text to Speech v1 API Explorer.
Please raise this as an improvement request on the github repo for the nodes, we can continue the discussion there. No promises, but I am open to accepting a pull request, if done right.

how to send correct curl command to webserver

So I got the data that is being sent to a specific server. Now I want to do the same using curl from my local machine to play around with specific repsonses from the server and learn more about curl as well.
Here is part of my data
POST /auth HTTP/1.1
platform: android
X-Auth-Token: <censored>
Content-Type: application/json; charset=utf-8
Host: api.blabla.com
Accept-Encoding: gzip
And the data that is being sent:
{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}
Now when I try cURL in my dos shell, I try
curl --insecure -X POST https://api.blabla.com/auth --data '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}'
The response I get from cURL is this:
{"code":401,"error":"blablaTokenRequired"}
Even though I specified the token. So there are two possible scenarios because the token is correct:
It has something to do with the SSL thing? (I use --insecure because I get an SSL error otherwise)
Something about my command is not correct but I can't figure out what.
Can someone kindly help me out? I am trying everything I can without success
I am not sure if I understand your application specific right, but probably one thing you need to take into account:
man curl says:
-d, --data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when
a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the
server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
-d, --data is the same as --data-ascii. --data-raw is almost the same but does not have a special interpreta‐
tion of the # character. To post data purely binary, you should instead use the --data-binary option. To URL-
encode the value of a form field you may use --data-urlencode.
As I can't see in your example the necessity of sending data as HTML form input, probably your application expects just a "raw" POST body and then you have to try this:
curl --insecure -X POST https://api.blabla.com/auth --data--binary '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}'
PS and for sure this is error is not about using --insecure which just asks curl to neglect ssl verification
you forgot the headers and enabling compressed encoding (gzip), however, i believe you can't force curl to only support gzip encoding using the curl command line alone, you will have to use libcurl, this will make the request say "Accept-Encoding: gzip,deflate" on most systems, using --compressed .. if that's not acceptable to you, rewrite it using libcurl (where you can force it to say only "gzip", if you wish, via CURLOPT_ENCODING )
curl -X POST https://api.blabla.com/auth --data '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}' --header 'platform: android' --header 'X-Auth-Token: <censored>' --header 'Content-Type: application/json; charset=utf-8' --header 'Host: api.blabla.com' --compressed
another gotcha: on some systems, there will be a default useragent header (like debian 6), while on some systems, curl comes without a default useragent (like debian 8).. you might want to use --user-agent '' too