Comments (11)
No. It accepts application/json
, but you must tell it you are sending that using the Content-type
in the POST
header.
from pengines.
TRIGGER WARNING - Java below!
void doAsk(Query query, String ask) throws PengineNotReadyException {
state.must_be_in(PSt.IDLE, PSt.ASK);
URL url = po.getActualURL("send");
StringBuffer response;
// TODO can we abstract this?
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// above should get us an HttpsURLConnection if it's https://...
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "JavaPengine");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-type", "application/json");
String urlParameters = po.getRequestBodyAsk(this.getID(), ask);
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
try {
wr.writeBytes(urlParameters);
wr.flush();
} finally {
wr.close();
}
int responseCode = con.getResponseCode();
if(responseCode < 200 || responseCode > 299) {
throw new PengineNotAvailableException("bad response code (if 500, perhaps query was invalid? Or query threw Prolog exception?)" + Integer.toString(responseCode));
}
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuffer();
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} finally {
in.close();
}
JsonReaderFactory jrf = Json.createReaderFactory(null);
JsonReader jr = jrf.createReader(new StringReader(response.toString()));
JsonObject respObject = jr.readObject();
JsonString eventjson = (JsonString)respObject.get("event");
String evtstr = eventjson.getString();
// TODO need to use this much of it to probe
if(respObject.containsKey("answer")) {
handleAnswer(respObject.getJsonObject("answer"));
}
} catch (IOException e) {
state.destroy();
throw new PengineNotAvailableException(e.toString());
} catch(SyntaxErrorException e) {
state.destroy();
throw new PengineNotAvailableException(e.getMessage());
}
}
from pengines.
/pengines/create accepts but /pengines/send does not.
On the Java side I'm adding Accept: application/json and Content-Type: application/json headers
I assumed this was me, of course, at first, but the code's pretty clear on the server side. I stepped into it.
http handler calls http_pengine_send
first clause fails.
Second clause calls http_parameters, which is eventually going to call http_parms which calls form_data_content_type which in turn needs application/x-www-form-urlencoded
http_pengine_create handles this differently
http_pengine_create's 2nd clause does this
memberchk(content_type(CT), Request),
sub_atom(CT, 0, _, _, 'application/json'), !,
http_read_json_dict(Request, Dict),
and is handled correctly.
The code I'm using is essentially copy/paste from my create method.
Java involves much typing. 8cO
from pengines.
True. /pengine/send expects a Prolog event. Might not be a great id. See the web/js/pengines.js
JavaScript client:
Pengine.prototype.send = function(event) {
var pengine = this;
$.ajax({ type: "POST",
url: pengine.options.server +
'/send?format=' + this.options.format +
'&id=' + this.id,
data: event + " .\n",
contentType: "application/x-prolog; charset=UTF-8",
success: function(obj) {
pengine.process_response(obj);
},
error: function(jqXHR, textStatus, errorThrown) {
pengine.error(jqXHR, textStatus, errorThrown);
}
});
};
The JavaScript client is probably the best documentation :) One route might be for you to complete the Java client, and before you start other ones, evaluate the oddities and possibly fix them.
from pengines.
OK - thanks for advice about JS client. I haven't looked at it - I've been following the main and Pengines sites and tracing through the Prolog implementation.
Are query strings allowed with POST ? RFC7231 is unclear on the subject, but I suspect it's buried somewhere. Obviously it's common to pass in body because that's what web forms do.
Anyway, I'll change JavaPengines to do this, and submit a fix that lets it use JSON.
from pengines.
And it won't respond with JSON either, even if the accept header is present it responds with raw Prolog.
from pengines.
Again, see pengines.js:
$.ajax({ type: "POST",
url: pengine.options.server +
'/send?format=' + this.options.format +
'&id=' + this.id,
I.e, it sends a `format' parameter. Not sure this is a good idea. Some of this is history :(
Do you keep a record of oddities?
from pengines.
Ah! Thought that was format TO server
thanks.
Not sure what's an oddity, though the API seems a bit jumbled.
from pengines.
@Anniepoo Is there still a remaining issue here or can this be closed?
from pengines.
leave open - its still true. having a workaround isnt a fix
from pengines.
i expect to fix in next week or so
from pengines.
Related Issues (20)
- Add README and LICENCE HOT 2
- Demo server out of memory HOT 1
- finding the javascript docs is still painful HOT 1
- not really destroying HOT 2
- plDoc and whitelisting HOT 11
- Oddity with JSON response to next HOT 2
- Alias?
- method of pulling output is painful HOT 1
- Created a Dockerfile - please include HOT 5
- Trouble loading run.pl HOT 1
- Pengines website down HOT 4
- ERROR: service `true' does not exist HOT 1
- http://pengines.swi-prolog.org/ internal error HOT 1
- Error No permission to call sandboxed `thread_send_message(_7532,_7534,_7536)' HOT 3
- Does pengines support DCG? HOT 1
- Error: Refusing to run HTTP server as root: No permission to open server `http` HOT 2
- Strange behaviour when solving goals with two answer vars HOT 5
- Simple JS functions on top of the Pengines JS API? HOT 1
- queens demo broken HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pengines.