6

I have a server running under NekoVM which provide a RESTLike service. I am trying to send a PUT/DELETE request to this server using the following Haxe code :

static public function main()
{
    var req : Http = new Http("http://localhost:2000/add/2/3");
    var bytesOutput = new haxe.io.BytesOutput();

    req.onData = function (data)
    {
        trace(data);
        trace("onData");
    }

    req.onError = function (err)
    {
        trace(err);
        trace("onError");
    }

    req.onStatus = function(status)
    {
        trace(status);
        trace("onStatus");
        trace (bytesOutput);
    }

    //req.request(true); // For GET and POST method

    req.customRequest( true, bytesOutput , "PUT" );

}

The problem is that only the onStatus event is showing something :

Main.hx:32: 200
Main.hx:33: onStatus
Main.hx:34: { b => { b => #abstract } }

Can anyone explain me what I am doing wrong with customRequest ?

Maxime Mangel
  • 1,906
  • 16
  • 18
  • customRequest may be asynchrone so maybe you shall wait a little bit for the callback before exiting the main() function? – Martin Delille Dec 09 '14 at 10:27
  • I tried by adding a Sys.Sleep(seconds) after customRequest. But nothing more. It's worth noting that request(true) is calling customRequest from Haxe sources. And it's working without a pause. – Maxime Mangel Dec 09 '14 at 10:39
  • Depending the framework, sleeping the program may also block callback. Do you have a working example of *onData*? – Martin Delille Dec 09 '14 at 10:44
  • Yes if I comment ```customRequest``` and uncomment ```request(true)``` the onData, is working. – Maxime Mangel Dec 09 '14 at 10:54
  • Maybe your custom request is invalid? Can you try a request with another tool like http://curl.haxx.se/? – Martin Delille Dec 10 '14 at 09:34

2 Answers2

3

customRequest does not call onData.

After customRequest call is finished either onError was called or first onStatus was called, then response was written to specified output.

stroncium
  • 1,430
  • 9
  • 8
1

For those finding these answers (@stroncium's is the correct one) and wondering what the finished code might look like:

static public function request(url:String, data:Any) {
    var req:Http = new haxe.Http(url);
    var responseBytes = new haxe.io.BytesOutput();

    // Serialize your data with your prefered method
    req.setPostData(haxe.Json.stringify(data)); 
    req.addHeader("Content-type", "application/json");

    req.onError = function(error:String) {
        throw error;
    };

    // Http#onData() is not called with custom requests like PUT

    req.onStatus = function(status:Int) {
        // For development, you may not need to set Http#onStatus unless you are watching for specific status codes
        trace(status);
    };

    // Http#request is only for POST and GET
    // req.request(true);

    req.customRequest( true, responseBytes, "PUT" );

    // 'responseBytes.getBytes()' must be outside the onStatus function and can only be called once
    var response = responseBytes.getBytes();

    // Deserialize in kind
    return haxe.Json.parse(response.toString());
}

I made a gist

dmcblue
  • 353
  • 2
  • 10