Can I write and fetch a resource on my public and private folder using rdflib?

I want to write some data to my pod and fetch them later. Can I do this using rdflib? Also if I want to save the data in a file, how should I proceed? I am using expressJS.

Here are two different ways to write data to a Pod with rdflib. They both create the same data and can be retrieved anytime later with await fetcher.load(url);

const $rdf = global.$rdf = require('rdflib');                                   
const store = $rdf.graph();                                                     
                                                                                
// This uses solid-node-client, you can also do the same with any               
// other auth/fetch library such as solid-client-authn-browser                  
const client = new (require("solid-node-client").SolidNodeClient)();            
const fetcher = $rdf.fetcher(store,{fetch:client.fetch.bind(client)});          
                                                                                
// Change these to locations in your Pod you have access to                     
const file1 = "file://"+process.cwd()+"/new-file1.ttl";                         
const file2 = "file://"+process.cwd()+"/new-file2.ttl";                         
                                                                                
async function main(){                                                          
  await client.login(); // add your credentials                               
                                                                                
  // write Turtle text to a file on a pod                                       
  await fetcher.webOperation( "PUT" , file1, {                                  
    body:"<#X> <#likes> <#Y>.",                                                 
    "contentType":"text/turtle",                                                
  });                                                                           
                                                                                
  // create and populate an in-memory store
  // and write it to a file on a pod     
  let doc = $rdf.sym(file2);                                                    
  let ns = $rdf.Namespace(file2+'#');                                           
  store.add( ns("X"), ns("likes"), ns("Y"), doc );                              
  await fetcher.putBack(doc);                                                   
}                                                                               
main();  

Just a note for future readers: if you’re working in Node, you’ll want @inrupt/solid-client-authn-node, not @inrupt/solid-client-authn-browser.

Well, that’s not how I would put it . :slight_smile: [disclaimer, I am the author of solid-node-client and @Vincent works for the company that makes solid-client-authn-node. ]

If you are working in node and you want to use rdflib against the local filesystem, or you want to be able to use either token or username/password logins, you should use solid-node-client. It has many features designed to work specifically with rdlib in the local filesystem. It is a superset of solid-client-authn-node, solid-auth-fetcher, and solid-rest, including all three libraries in its distro and providing all three fetches in different circumstances. For token-style logins, it transparently uses Inrupt’s solid-client-authn node. It use the other libraries for other kinds of connections that solid-client-authn-node does not support. If you don’t need the local interaction or the username/password, either solid-client-authn-node or solid-node-client can be used.

The point of my comment about solid-client-authn-browser is that rdlib is agnostic about whether it is used in node or in a browser so it can wotk with any of the auth/fetch libraries including both the node and browser versions of Inrupt’s solid-client-authn.

2 Likes

Thanks. Unfortunately, I was having some server issue for dependency confliction. Instead of rdflib, I am using solid-node-client now. I want to save json data to my solidCommunity pod in private. I followed documentation of solid-node-client. How can I do this? I can append txt file easily but having “status: 415” for json file.
My code:

  let writeResponse = await client.fetch(`${updatedUserId}private/credentials.json`, {
        method: "PUT",
        body: credenJson, // json object
        headers: {
            "Content-type": 'application/json'
        }
    });

@Tapu106 415 means “unsupported media type” so I would try with a different content type.

You body is a json Object.
Your body should be a JSON.stringify jsonObject.

1 Like

I did. But still does not work.

I have added .json resources in Solid server. So I know it works.
With solid-ide for example, but not only. Tiddlywiki for solid used .json files before being converted to use .ttl files

Have you tried with a very simple :
body: ‘{}’

This is the the way solid-ide create a new .json file. Using solid-file-client.

@bourgeoa - what content-type do you use?

application/json contentType

1 Like

So the 415 was caused by wrong content-type of the body, not by specifying the wrong content type in the header.

I can upload json file. But how to view them? sorry I am noob. My file:
Screenshot from 2021-07-10 01-26-54

let response = await client.fetch( URL );
console.log(  await response.text() );

I have used solid-node-client for saving files on pod using GitHub documentation. is there no way I can view my son file using solidcommunity?

From the github documentation :

Read and Write : fetch()

// now read it
let readResponse = await client.fetch( 'https://example.com/file.txt' );
// display its contents
console.log( await readResponse.text()  );

Sorry, my question was not clear. I was asking about how can I view the json data on browser using solidcommunity service. I can view the txt file just fine
my text file:

How can I view my json file as text given above?

If you want to see that text using solid-node-client you do it as shown above. If you are in an express app, instead of using console.log, send it to an HTML div. If I still haven’t understood your question, please ask again.

If I understand your question correctly :
Can I display a resource.json with the solicommunity.net browser. Called mashlib or Solid.

The response is no you cannot. I suppose for historical reasons. It is on my to do list .json and .jsonld

There are other ways to do that. Some but not limited to :

  • solid-ide
  • inrupt browser
  • popock
  • Solid Filemanager
  • … other ide I do not have the name here