Search code examples
dartdart-html

How to get the HTTP response text from a HttpRequest.postFormData's catchError's _XMLHttpRequestProgressEvent?


Given the following pseudo code:

import "dart:html";

HttpRequest.postFormData(url, data).then((HttpRequest request) {

    ...

}).catchError((error) {

    // How do I get the response text from here?

});

If the web server replies with a 400 BAD REQUEST then the catchError will be invoked. However, the error parameter is of the type _XMLHttpRequestProgressEvent which apparently doesn't exist in Dart's library.

So, how do I get the response text from the 400 BAD REQUEST response that was sent from the web server?


Solution

  • It seems like the target in your error object is actually your HttpRequest.

    You may find this link helpful: https://www.dartlang.org/docs/tutorials/forms/#handling-post-requests

    You could do something like:

    import "dart:html";
    
    HttpRequest.postFormData(url, data).then((HttpRequest request) {
        request.onReadyStateChange.listen((response) => /* do sth with response */);
    }).catchError((error) {
        print(error.target.responseText); // Current target should be you HttpRequest
    });