How to connect with the HTTP Client

edited July 2016 in Kinoma Element

In my main.js, I created a method called pressPost, it posts to a url when a button is pressed.

My assumption is that I create a transfer complete listener on the request and when he request returns it will be called, however, nothing happens. Am I doing something wrong here? My assumption is that it would be called regardless of success or failure of the request (like on a code 200 or a 404).

function pressPost() {

let request = new HTTPClientRequest("https://xxxxx.com/restful_endpoint");
request.method = "POST";
request.setHeader("Content-Type", "application/JSON");
trace(request);

    //  Attempting to post some data in the body of the request
    request.start(JSON.stringify(
{
    "type": "basic+press",
    "date": "\"2016-07-27T23:16:40.859Z\"",
    "user_id": "parent"
}));

request.onTransferComplete = success => {
    trace("success: "+success+"\n") 
}

}

Thanks, Peter

Answers

  • Answer ✓

    onTransferComplete is called in 2 cases:

    1) The connection to the server is closed (in which case success will be true)
    2) There is an error (in which case success will be false)

    If the server uses HTTP version 1.0, onTransferComplete will always be called. Otherwise you can make the server close the connection by adding request.addHeader("Connection", "Close");.

  • edited July 2016

    This worked for me. Thank you for the solution!

    I don't know if this is my inexperience or something everyone should know, but it seems as though it would be helpful to future coders if the documentation reflected this piece of information, ie "If the server uses HTTP version 1.0, onTransferComplete will always be called. Otherwise you can make the server close the connection by adding request.addHeader("Connection", "Close");"

  • edited August 2016

    This worked for me for making the onTransferComplete get called, but the data in the request doesn't seem to be being posted. I've tried the API i created on the Platform as a Service site BankAnd and I've also tried http://www.hashemian.com, a service that just saves what was posted in your request. Both seem to indicate that nothing is being posted. Is there some other trick to posting my json? I've also tried changing the content type and that doesn't help either. The data sent as the request body just doesn't seem to be getting there.

    With the addition of th e request.addHeader("Connection", "Close");", does the code above seem like it ought to work?

  • Ah, apparently the Content-Length header needs to be set. With that addition, the data is being sent correctly.

  • Excellent! Glad you got everything working.

    You are right that it would be helpful for the documentation to reflect the information about the "close" connection option--I've added a note in the HTTPClientRequest section of the Programmer's Guide to Kinoma Element.

    And I was just typing a response to recommend you add the Content-Length header field, but you beat me to it :) In case anyone else reads this and wants some sample code to make the Hashemian form post tester work, here's a sample to do that:

    import HTTPClientRequest from "HTTPClient";
    
    var main = {
        onLaunch() {
            let request = new HTTPClientRequest("http://www.hashemian.com/tools/form-post-tester.php/"); 
            request.method = "POST";
            request.setHeader("Content-Type", "application/JSON");
            request.addHeader("Connection", "Close");
    
            let data = JSON.stringify({ "test": 123 });
            request.setHeader("Content-Length", data.length);
    
            request.onTransferComplete = response => { 
                let data = String.fromArrayBuffer(request.content);
                trace(`Data returned: ${data}\n`);
            } 
    
            request.start(data);
        },
    }
    
    export default main;
    

    Also, these may be useful:

Sign In or Register to comment.