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?
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.
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.
HTTP Response broadly has 3 main components: Status Line. Headers. Body (Optional)
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.
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With