Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dart

dart-html

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?

like image 397
corgrath Avatar asked Feb 17 '14 21:02

corgrath


People also ask

What is a HTTP response body?

An HTTP response object typically represents the HTTP packet (response packet) sent back by Web Service Server in response to a client request. An HTTP Response contains: A status. Collection of Headers. A Body.

What is the difference between HTTP request and HTTP response?

HTTP messages are how data is exchanged between a server and a client. There are two types of messages: requests sent by the client to trigger an action on the server, and responses, the answer from the server.

What are the three parts of an HTTP response?

HTTP Response broadly has 3 main components: Status Line. Headers. Body (Optional)

What is HTTP request and HTTP response with example?

What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. Example: A client (browser) sends an HTTP request to the server; then the server returns a response to the client.


1 Answers

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
});
like image 101
markovuksanovic Avatar answered Nov 11 '22 02:11

markovuksanovic