San*_*.91 6 api http dart flutter
通过 http.dart 在 flutter 上调用 post API 时,出现以下错误:
HttpException:在收到完整标头之前连接已关闭,uri = http://192.168.3.218:12225/resourcabc/subresourceXYZ
当邮递员给出正确的回复时为什么会这样?我之前也看到有人问过这个问题。另外,我正在物理设备上进行调试:三星
这是代码
Future<Map> post(
String path, {
String token,
dynamic body,
bool parseResponse: false,
isFormData: false,
isUseBaseURL: false,
isEncoded: false,
}) async {
Uri uri;
uri =
isUseBaseURL ? Uri.parse('${Paths.baseUrl}/$path') : Uri.parse('$path');
print(uri.toString());
print(body.toString());
var request =
await client.postUrl(uri).timeout(const Duration(seconds: 30));
if (token != null) {
request.headers.add(HttpHeaders.authorizationHeader, 'Bearer $token');
} else {
print('token is null');
request.headers.add("X-Consumer-Custom-ID", "96");
}
if (body != null) {
if (isFormData) {
request
..headers.contentType = new ContentType(
'application', 'x-www-form-urlencoded',
charset: 'utf-8')
..write(body);
} else {
request
..headers.contentType = ContentType.json
..write(isEncoded ? body : json.encode(body));
}
}
print(json.encode(body));
print('Sending data');
var response = await request.close();
print(response.statusCode);
Map responseMap = await _extractJson(response);
_checkAndThrowError(response, responseMap);
return responseMap;
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
4491 次 |
| 最近记录: |