GithubHelp home page GithubHelp logo

Comments (11)

JanWielemaker avatar JanWielemaker commented on July 30, 2024

No. It accepts application/json, but you must tell it you are sending that using the Content-type in the POST header.

from pengines.

Anniepoo avatar Anniepoo commented on July 30, 2024

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.

Anniepoo avatar Anniepoo commented on July 30, 2024

/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.

JanWielemaker avatar JanWielemaker commented on July 30, 2024

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.

Anniepoo avatar Anniepoo commented on July 30, 2024

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.

Anniepoo avatar Anniepoo commented on July 30, 2024

And it won't respond with JSON either, even if the accept header is present it responds with raw Prolog.

from pengines.

JanWielemaker avatar JanWielemaker commented on July 30, 2024

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.

Anniepoo avatar Anniepoo commented on July 30, 2024

Ah! Thought that was format TO server
thanks.
Not sure what's an oddity, though the API seems a bit jumbled.

from pengines.

wouterbeek avatar wouterbeek commented on July 30, 2024

@Anniepoo Is there still a remaining issue here or can this be closed?

from pengines.

Anniepoo avatar Anniepoo commented on July 30, 2024

leave open - its still true. having a workaround isnt a fix

from pengines.

Anniepoo avatar Anniepoo commented on July 30, 2024

i expect to fix in next week or so

from pengines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.