Skip to content

Commit

Permalink
better accept header parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Forberg committed Feb 1, 2024
1 parent c8294d8 commit 0ce854a
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions server/app/common/get-linked-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@ module.exports = async function getLinkedData(req, res, next, resourceUri, templ
accept = HttpStrings.CONTENT_TYPE_JSONLD;
}

if(accept == HttpStrings.CONTENT_TYPE_JSONLD) {
let indexOfComma = accept.indexOf(",");
let firstAccept = accept.substring(0, indexOfComma);

if (firstAccept == HttpStrings.CONTENT_TYPE_JSONLD) {
// Handle JSONLD separately
var jsonld = await getJsonld(resourceUri, template, formatting);

if(jsonld != null) {
if (jsonld != null) {
res.set(HttpStrings.HEADER_CONTENT_TYPE, HttpStrings.CONTENT_TYPE_JSONLD);
res.status(200).send(`${JSON.stringify(jsonld, null, 3)}\n`);
return;
}
} else {

// Format the query
var query = ServerUtils.formatQuery(template, {
RESOURCE_URI: resourceUri
});

// Prepare OPTIONS for database request
var headers = {};
headers[HttpStrings.HEADER_CONTENT_TYPE] = HttpStrings.CONTENT_TYPE_FORM_URL_ENCODED;
headers[HttpStrings.HEADER_ACCEPT] = accept;

var options = {
method: HttpStrings.METHOD_POST,
uri: `${process.env.DATABUS_DATABASE_URL}/sparql?timeout=10000`,
body: `query=${encodeURIComponent(query)}`,
headers: headers,
};

// Pipe the request
request(options).pipe(res);
return;
}

res.status(500).send('Encountered a database error when trying to fetch the resource.');
// Format the query
var query = ServerUtils.formatQuery(template, {
RESOURCE_URI: resourceUri
});

// Prepare OPTIONS for database request
var headers = {};
headers[HttpStrings.HEADER_CONTENT_TYPE] = HttpStrings.CONTENT_TYPE_FORM_URL_ENCODED;
headers[HttpStrings.HEADER_ACCEPT] = accept;

var options = {
method: HttpStrings.METHOD_POST,
uri: `${process.env.DATABUS_DATABASE_URL}/sparql?timeout=10000`,
body: `query=${encodeURIComponent(query)}`,
headers: headers,
};

// Pipe the request
request(options).pipe(res);
return;
}

0 comments on commit 0ce854a

Please sign in to comment.