Amareis / another-rest-client

Simple pure TypeScript REST API client that makes your code lesser and more beautiful than without it.
MIT License
174 stars 16 forks source link

ProgressEvent #14

Closed macfire closed 6 years ago

macfire commented 6 years ago

Is it possible to get the progress of a (large) JSON response?

xhr spec xhr example

Update: I've tried implementing the example on whatwg.org, but no success. I don't see any calls to onprogress in my browser console. Would this be something provided by the server? (I'm using Python/Flask, and doing some research there also).

api.api().myapi()
    .get()
    .on('response', function(res) {
        console.log(res.onprogress) // returns 'null'
        res.onprogress = function(pe) {
           console.log(pe); // not reached
           if (pe.lengthComputable) {
               console.log(pe.loaded) // not reached
           }}})
     .then(function (_json) { do_stuff(_json) })
Amareis commented 6 years ago

You need to listens request event, not response, because response firing after downloading completed.

Amareis commented 6 years ago

Your server should set Content-Length header for working lengthComputable, loaded and total XHR properties.

macfire commented 6 years ago

Thanks. I'll find out how to set Content-Length header and try again.

macfire commented 6 years ago

I did discover that the Content-Length is already being set. I tried putting the code in api.on('request'), but the onprogress method is not available.

I am going to close this thread for now. If I find a solution, I'll comment here.