Open davidascher opened 8 years ago
After some wire sniffing, it seems that the request
module by default (now?) sends the data as json with a Content-Type
of application/json
, which seems to confuse cayley.
Changing the content-type in client.js
to text
fixes my test.
I don't think that's the right content type, but I'm not sure what is.
Maybe @barakmich knows.
I add the headers content-type: text/plain and many other values, the query result is still null. And then I write a test use request module
var request = require('request');
request.post({
uri: 'http://localhost:64210/api/v1/query/gremlin',
body: 'g.V("Subject1").All()',
json: true
}, function(err, res, body) {
console.log('test---',res.request, '-----test')
})
// requst with param json: true, I get the result null
Request {
// ....
body: '"g.V(\\"Subject1\\").All()"',
// ...
}
There is a pair of superfluous commas before g.V... So I comment the json: true
in cayley/lib/v1/gremlin.js
line15, and add body = JSON.parse(body)
in callback line 17, It worked.
Thank you @davidascher !
With the String Template literals
of ES2015, finaly I found maybe I can query easily without cayley.js , and send gremlin js code block as post body...
query(text) {
return new Promise((resolve, reject) => {
this.ctx.curl('http://localhost:64210/api/v1/query/gremlin', {
method: 'POST',
contentType: 'json',
data: text
})
.then((res) => {
if (typeof res == 'object' && res.data) {
try {
var result = JSON.parse(res.data.toString()).result
if (result) {
resolve(result)
} else {
resolve([])
// [cayley]: node may not exist
}
} catch(err) {
reject(err)
}
} else {
reject(new Error('[cayley]: response data error, type error'))
}
})
.catch((error) => {
console.error(error)
})
});
}
Using this test:
I get null results in the last function, but the triple gets written to the database as visible using the HTTP user interface.
Maybe something in the latest cayley db broke this library?