我有这个简单的飞镖片段:
import 'package:http/http.dart' as http;
Client _http = new BrowserClient();
final response = await _http
.post(url, headers: someHeader, body: someValue);
当前请求的服务器,返回Bad Request 400响应。由于某些原因,我希望能够获得状态码。但是一旦_http.post被调用,我就会收到此错误:
POST http://localhost/path 400 (Bad Request)
将块放入try/catch没有帮助,因为捕获的异常ClientException没有关于状态代码和响应主体的信息(这在这里很重要)。我怎样才能在这里处理状态码,而不抛出异常?
更新:
通话后我有这个片段_http.post():
if (response.statusCode == 400) {
// manage bad request
}
但是responseis null,所以statusCode这里无法访问。
更新 2:
我在服务器上收到请求并可以在服务器上进行跟踪。正如我所提到的,服务器正在发送400 Bad Request响应,并且工作正常。但是在 Dart 中获取响应会导致错误。