[英]Flutter HTTP request send using xml body
我正在嘗試在 Flutter/Dart 中處理 HTTP 請求(在這種情況下,使用 SEARCH 方法過濾 WebDAV 服務器(Nextcloud)上的一些文件)需要在請求正文中發送 XML 數據。
[x] 可以通過 --data 參數在終端上使用 cURL 執行命令:
curl -u user:pass -X SEARCH 'https://host123.com.br/remote.php/dav' -H "content-Type: text/xml" --data '<?xml version="1.0" encoding="UTF-8"?><d:searchrequest xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns"><d:basicsearch><d:select><d:prop><d:displayname/></d:prop></d:select><d:from><d:scope><d:href>/files/wprech</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:and><d:or><d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>image/png</d:literal></d:eq><d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>image/jpg</d:literal></d:eq><d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>video/mp4</d:literal></d:eq></d:or></d:and></d:where><d:orderby/></d:basicsearch></d:searchrequest>'
[x] 也可以通過 Postman 應用程序工作:
[ ] 無法使用 Flutter/Dart 對 xml 主體執行此請求。 這個項目的所有其他 HTTP 請求我們使用了 DIO pkg,它工作正常,但問題是。 與它一起發送 xml 主體。 最接近的代碼如下:
void _list() async {
final prefs = await SharedPreferences.getInstance();
var us = prefs.getString('id') ?? '';
var sn = prefs.getString('password') ?? '';
String basicAuth = 'Basic ' + base64Encode(utf8.encode('$us:$sn'));
try {
Dio dio = new Dio();
dio.options.method = 'SEARCH';
dio.options.responseType = ResponseType.plain;
dio.options.headers = {
HttpHeaders.authorizationHeader: basicAuth,
'content-Type': 'text/xml'
};
String data =
'<?xml version="1.0" encoding="UTF-8"?><d:searchrequest xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns"><d:basicsearch><d:select><d:prop><d:displayname/></d:prop></d:select><d:from><d:scope><d:href>/files/wprech</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:and><d:or><d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>image/png</d:literal></d:eq><d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>image/jpg</d:literal></d:eq><d:eq><d:prop><d:getcontenttype/></d:prop><d:literal>video/mp4</d:literal></d:eq></d:or></d:and></d:where><d:orderby/></d:basicsearch></d:searchrequest>';
Response response = await dio.request(
"https://host123.com.br/remote.php/dav",
data: data);
print(response);
} catch (e) {
print(e);
}}
服務器響應在 400、404、500 和 501 之間變化,具體取決於它的發送方式:
I/flutter ( 6767): DioError [DioErrorType.RESPONSE]: Http status error [400]
有什么幫助嗎? :)
嘗試使用更簡單的package:http
版本。
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
main() async {
var username = 'foo';
var password = 'B@r!';
var credential = base64.encode(utf8.encode('$username:$password'));
var client = http.IOClient();
var request = http.Request(
'SEARCH',
Uri.parse('https://host123.com.br/remote.php/dav'),
);
request.headers.addAll({
HttpHeaders.authorizationHeader: 'Basic $credential',
'content-type': 'text/xml' // or text/xml;charset=utf-8
});
var xml = '<?xml version="1.0" encoding="UTF-8"?>...';
// either
request.body = xml;
// which will encode the string to bytes, and modify the content-type header, adding the encoding
// or
// request.bodyBytes = utf8.encode(xml);
// which gives you complete control over the character encoding
var streamedResponse = await client.send(request);
print(streamedResponse.statusCode);
var responseBody =
await streamedResponse.stream.transform(utf8.decoder).join();
print(responseBody);
client.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.