COMMUNITY

Invalid Chunk while fetching data from Rest Service using Haxe

I am using Haxe http-client library and below is the code I have written:

class Main
{
    static public function main() {
        var request = new HttpRequest();
        request.method = "GET";
        request.url = new Url("MY_URL");
        request.headers.set("Content-Type", "application/json");
        request.send({
            onData: function (data : String) {
                trace("In Data :");
                trace(data);
            },
            onError: function (error : String, ?data : String) {
                trace(error);
            },
            onStatus: function (code : Int) {
                trace("Status " + code);
            }
        });
    }
}

When I try to execute this after converting to any language, I get the below error:

Main.hx:20: Invalid chunk

It seems that the data is large, but how do I get this data fully? Is it possible to handle this data in streams or bytes?

I think you can’t get a stream from the HTTP response (to be confirmed !): there is no such API with the Http client or the standard library. But you can get the full response as bytes with the Http.onBytes handler.

Beware: the documentation is not up to date. It says “containing the result String” but it’s really a Bytes instance that is passed. Http.onData is just a convenient handler converting the Http.onBytes result to a string.

See: https://github.com/HaxeFoundation/haxe/blob/4.1.4/std/haxe/http/HttpBase.hx#L232

Can you post the response headers and its body? You get this error when there is a Transfer-Encoding: chunked header and… (well, I don’t really understand what the code is doing!).

See: https://github.com/HaxeFoundation/haxe/blob/4.1.4/std/sys/Http.hx#L415

I assume (from your other post) the response used chunked encoding and the body is empty. I think that is invalid according to the doc.

Data is sent in a series of chunks. The Content-Length header is omitted in this case and at the beginning of each chunk you need to add the length of the current chunk in hexadecimal format, followed by ’\r\n ’ and then the chunk itself, followed by another ’\r\n '. The terminating chunk is a regular chunk, with the exception that its length is zero. It is followed by the trailer, which consists of a (possibly empty) sequence of entity header fields.

I think the code you are using expects at least one chunk (can be empty chunk) in the body.